`

利用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. <project xmlns="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.0 http://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-demo Maven Webapp</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>maven repo</id>  
  18.             <name>maven repo</name>  
  19.             <url>http://repo1.maven.org/maven2/</url>  
  20.         </repository>  
  21.         <!-- For resteasy -->  
  22.         <repository>  
  23.             <id>jboss</id>  
  24.             <name>jboss repo</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.             <!-- filter out unwanted jars -->  
  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.   
  54.         <dependency>  
  55.             <groupId>org.jboss.resteasy</groupId>  
  56.             <artifactId>resteasy-jettison-provider</artifactId>  
  57.             <version>2.2.1.GA</version>  
  58.             <exclusions>  
  59.                 <exclusion>  
  60.                     <groupId>javax.xml.bind</groupId>  
  61.                     <artifactId>jaxb-api</artifactId>  
  62.                 </exclusion>  
  63.                 <exclusion>  
  64.                     <groupId>com.sun.xml.bind</groupId>  
  65.                     <artifactId>jaxb-impl</artifactId>  
  66.                 </exclusion>  
  67.                 <exclusion>  
  68.                     <groupId>javax.xml.stream</groupId>  
  69.                     <artifactId>stax-api</artifactId>  
  70.                 </exclusion>  
  71.             </exclusions>  
  72.         </dependency>  
  73.     <dependency>  
  74.       <groupId>junit</groupId>  
  75.       <artifactId>junit</artifactId>  
  76.       <version>3.8.1</version>  
  77.       <scope>test</scope>  
  78.     </dependency>  
  79.   </dependencies>  
  80.  <build>  
  81.         <finalName>rest-resteay-demo</finalName>  
  82.         <plugins>  
  83.             <plugin>  
  84.                 <groupId>org.apache.maven.plugins</groupId>  
  85.                 <artifactId>maven-compiler-plugin</artifactId>  
  86.                 <configuration>  
  87.                     <source>1.6</source>  
  88.                     <target>1.6</target>  
  89.                 </configuration>  
  90.             </plugin>  
  91.         </plugins>  
  92.     </build>  
  93. </project>  
这个不是重点:看不懂这个pom.xml没关系,也就是下载依赖包,打包,先继续往下看

2.编写jax-rs的服务类

  1. package resteasy.server;  
  2.   
  3. import javax.ws.rs.GET;  
  4. import javax.ws.rs.Path;  
  5. import javax.ws.rs.PathParam;  
  6.   
  7. @Path(value = "echo")  
  8. public class Echo {  
  9.     @GET  
  10.     @Path(value = "/{message}")  
  11.     public String echoService(@PathParam("message") String message)  
  12.     {  
  13.         return message;  
  14.     }  
  15. }  
@Path表示开启访问这个资源的路径

@GET表示响应HTTP 的get方法

@PathParam表示引用URI中得参数

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

3.web.xml的配置

  1. <!DOCTYPE web-app PUBLIC  
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  3.  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  4.   
  5. <web-app>  
  6.     <context-param>  
  7.         <param-name>resteasy.resources</param-name>  
  8.         <param-value>resteasy.server.Echo</param-value>  
  9.     </context-param>  
  10.    <listener>  
  11.       <listener-class>  
  12.          org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap  
  13.       </listener-class>  
  14.    </listener>  
  15.   
  16.    <servlet>  
  17.       <servlet-name>Resteasy</servlet-name>  
  18.       <servlet-class>  
  19.          org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher  
  20.       </servlet-class>  
  21.    </servlet>  
  22.   
  23.    <servlet-mapping>  
  24.       <servlet-name>Resteasy</servlet-name>  
  25.       <url-pattern>/*</url-pattern>  
  26.    </servlet-mapping>  
  27. </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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics