resteasy简单的项目搭建



基于resteasy版本:2.2.1.GA

http://blog.csdn.net/rnzuozuo/article/details/38349403

使用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
[html] view plain copy
  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.0modelVersion>  
  4.   <groupId>rest.resteasygroupId>  
  5.   <artifactId>rest-resteay-demoartifactId>  
  6.   <packaging>warpackaging>  
  7.   <version>1.0version>  
  8.   <name>rest-resteay-demo Maven Webappname>  
  9.   <url>http://maven.apache.orgurl>  
  10.   <repositories>  
  11.         <repository>  
  12.             <id>java.netid>  
  13.             <url>http://download.java.net/maven/1url>  
  14.             <layout>legacylayout>  
  15.         repository>  
  16.         <repository>  
  17.             <id>maven repoid>  
  18.             <name>maven reponame>  
  19.             <url>http://repo1.maven.org/maven2/url>  
  20.         repository>  
  21.           
  22.         <repository>  
  23.             <id>jbossid>  
  24.             <name>jboss reponame>  
  25.             <url>http://repository.jboss.org/nexus/content/groups/public/url>  
  26.         repository>  
  27.     repositories>  
  28.   <dependencies>  
  29.     <dependency>  
  30.             <groupId>org.jboss.resteasygroupId>  
  31.             <artifactId>resteasy-jaxrsartifactId>  
  32.             <version>2.2.1.GAversion>  
  33.               
  34.             <exclusions>  
  35.                 <exclusion>  
  36.                     <groupId>commons-httpclientgroupId>  
  37.                     <artifactId>commons-httpclientartifactId>  
  38.                 exclusion>  
  39.                 <exclusion>  
  40.                     <groupId>javax.servletgroupId>  
  41.                     <artifactId>servlet-apiartifactId>  
  42.                 exclusion>  
  43.                 <exclusion>  
  44.                     <groupId>javax.xml.bindgroupId>  
  45.                     <artifactId>jaxb-apiartifactId>  
  46.                 exclusion>  
  47.                 <exclusion>  
  48.                     <groupId>com.sun.xml.bindgroupId>  
  49.                     <artifactId>jaxb-implartifactId>  
  50.                 exclusion>  
  51.             exclusions>  
  52.         dependency>  
  53.   
  54.         <dependency>  
  55.             <groupId>org.jboss.resteasygroupId>  
  56.             <artifactId>resteasy-jettison-providerartifactId>  
  57.             <version>2.2.1.GAversion>  
  58.             <exclusions>  
  59.                 <exclusion>  
  60.                     <groupId>javax.xml.bindgroupId>  
  61.                     <artifactId>jaxb-apiartifactId>  
  62.                 exclusion>  
  63.                 <exclusion>  
  64.                     <groupId>com.sun.xml.bindgroupId>  
  65.                     <artifactId>jaxb-implartifactId>  
  66.                 exclusion>  
  67.                 <exclusion>  
  68.                     <groupId>javax.xml.streamgroupId>  
  69.                     <artifactId>stax-apiartifactId>  
  70.                 exclusion>  
  71.             exclusions>  
  72.         dependency>  
  73.     <dependency>  
  74.       <groupId>junitgroupId>  
  75.       <artifactId>junitartifactId>  
  76.       <version>3.8.1version>  
  77.       <scope>testscope>  
  78.     dependency>  
  79.   dependencies>  
  80.  <build>  
  81.         <finalName>rest-resteay-demofinalName>  
  82.         <plugins>  
  83.             <plugin>  
  84.                 <groupId>org.apache.maven.pluginsgroupId>  
  85.                 <artifactId>maven-compiler-pluginartifactId>  
  86.                 <configuration>  
  87.                     <source>1.6source>  
  88.                     <target>1.6target>  
  89.                 configuration>  
  90.             plugin>  
  91.         plugins>  
  92.     build>  
  93. project>  
这个不是重点:看不懂这个pom.xml没关系,也就是下载依赖包,打包,先继续往下看

2.编写jax-rs的服务类

[java] view plain copy
  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的配置

[html] view plain copy
  1.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  2.  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  3.   
  4. <web-app> 

  5.     <context-param>  
  6.         <param-name>resteasy.resourcesparam-name>  
  7.         <param-value>resteasy.server.Echoparam-value>  
  8.     context-param>  
  9.    <listener>  
  10.       <listener-class>  
  11.          org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap   
  12.       listener-class>  
  13.    listener>  
  14.   
  15.    <servlet>  
  16.       <servlet-name>Resteasyservlet-name>   
  17.       <servlet-class>  
  18.          org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher  
  19.       servlet-class>  
  20.    servlet>  
  21.   
  22.    <servlet-mapping>  
  23.       <servlet-name>Resteasyservlet-name>  
  24.       <url-pattern>/*url-pattern>  
  25.    servlet-mapping>  
  26. web-app>  
配置响应的listener和servlet无非就是初始resteasy的服务(先简单理解)

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

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

你可能感兴趣的:(resteasy接口,maven,resteasy)