利用resteasy框架构建rest webservice----第一波:快速构建HelloWorld(实例、教程)

转载请标明出处:http://blog.csdn.net/caizhh2009/article/details/6934845

基于resteasy版本:2.2.1.GA

使用maven2.2.1作为构建和依赖管理工具

1.创建工程,配置pom.xml

mvn archetype:create -DgroupId=com.longtask.rest.easyrest -DartifactId=easyrest -DarchetypeArtifactId=maven-archetype-webapp

mvn eclipse:eclipse

注:使用m2eclipse插件可直接import
  1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>rest.resteasy</groupId>
  5. <artifactId>rest-resteay-demo</artifactId>
  6. <packaging>war</packaging>
  7. <version>1.0</version>
  8. <name>rest-resteay-demoMavenWebapp</name>
  9. <url>http://maven.apache.org</url>
  10. <repositories>
  11. <repository>
  12. <id>java.net</id>
  13. <url>http://download.java.net/maven/1</url>
  14. <layout>legacy</layout>
  15. </repository>
  16. <repository>
  17. <id>mavenrepo</id>
  18. <name>mavenrepo</name>
  19. <url>http://repo1.maven.org/maven2/</url>
  20. </repository>
  21. <!--Forresteasy-->
  22. <repository>
  23. <id>jboss</id>
  24. <name>jbossrepo</name>
  25. <url>http://repository.jboss.org/nexus/content/groups/public/</url>
  26. </repository>
  27. </repositories>
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.jboss.resteasy</groupId>
  31. <artifactId>resteasy-jaxrs</artifactId>
  32. <version>2.2.1.GA</version>
  33. <!--filteroutunwantedjars-->
  34. <exclusions>
  35. <exclusion>
  36. <groupId>commons-httpclient</groupId>
  37. <artifactId>commons-httpclient</artifactId>
  38. </exclusion>
  39. <exclusion>
  40. <groupId>javax.servlet</groupId>
  41. <artifactId>servlet-api</artifactId>
  42. </exclusion>
  43. <exclusion>
  44. <groupId>javax.xml.bind</groupId>
  45. <artifactId>jaxb-api</artifactId>
  46. </exclusion>
  47. <exclusion>
  48. <groupId>com.sun.xml.bind</groupId>
  49. <artifactId>jaxb-impl</artifactId>
  50. </exclusion>
  51. </exclusions>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.jboss.resteasy</groupId>
  55. <artifactId>resteasy-jettison-provider</artifactId>
  56. <version>2.2.1.GA</version>
  57. <exclusions>
  58. <exclusion>
  59. <groupId>javax.xml.bind</groupId>
  60. <artifactId>jaxb-api</artifactId>
  61. </exclusion>
  62. <exclusion>
  63. <groupId>com.sun.xml.bind</groupId>
  64. <artifactId>jaxb-impl</artifactId>
  65. </exclusion>
  66. <exclusion>
  67. <groupId>javax.xml.stream</groupId>
  68. <artifactId>stax-api</artifactId>
  69. </exclusion>
  70. </exclusions>
  71. </dependency>
  72. <dependency>
  73. <groupId>junit</groupId>
  74. <artifactId>junit</artifactId>
  75. <version>3.8.1</version>
  76. <scope>test</scope>
  77. </dependency>
  78. </dependencies>
  79. <build>
  80. <finalName>rest-resteay-demo</finalName>
  81. <plugins>
  82. <plugin>
  83. <groupId>org.apache.maven.plugins</groupId>
  84. <artifactId>maven-compiler-plugin</artifactId>
  85. <configuration>
  86. <source>1.6</source>
  87. <target>1.6</target>
  88. </configuration>
  89. </plugin>
  90. </plugins>
  91. </build>
  92. </project>
这个不是重点:看不懂这个pom.xml没关系,也就是下载依赖包,打包,先继续往下看

2.编写jax-rs的服务类

  1. packageresteasy.server;
  2. importjavax.ws.rs.GET;
  3. importjavax.ws.rs.Path;
  4. importjavax.ws.rs.PathParam;
  5. @Path(value="echo")
  6. publicclassEcho{
  7. @GET
  8. @Path(value="/{message}")
  9. publicStringechoService(@PathParam("message")Stringmessage)
  10. {
  11. returnmessage;
  12. }
  13. }
@Path表示开启访问这个资源的路径

@GET表示响应HTTP 的get方法

@PathParam表示引用URI中得参数

详细的注解可参考我下面的参考文档

3.web.xml的配置

  1. <!DOCTYPEweb-appPUBLIC
  2. "-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd">
  4. <web-app>
  5. <context-param>
  6. <param-name>resteasy.resources</param-name>
  7. <param-value>resteasy.server.Echo</param-value>
  8. </context-param>
  9. <listener>
  10. <listener-class>
  11. org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
  12. </listener-class>
  13. </listener>
  14. <servlet>
  15. <servlet-name>Resteasy</servlet-name>
  16. <servlet-class>
  17. org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
  18. </servlet-class>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>Resteasy</servlet-name>
  22. <url-pattern>/*</url-pattern>
  23. </servlet-mapping>
  24. </web-app>
配置响应的listener和servlet无非就是初始resteasy的服务(先简单理解)

3.打包部署到响应的servlet容器即可(如tomcat),然后访问http://localhost:8080/rest-resteay-demo/echo/hello,world,网页上出现hello,world则成功
hello,world可换成任意字符,同样也将返回响应的字符

注:如果不使用maven,则可以到resteasy官网下载响应jar包即可


demo下载


下一章预告:阐述不同的方式用resteasy发布我们的restful webservice 服务,有问题可跟帖,一起讨论,共同进步

参考文献:

1.resteasy官方文档

2.resteasy wiki

3.jax-rs api

4.The Java EE 6 Tutorial

你可能感兴趣的:(webservice)