转载请标明出处:http://blog.csdn.net/caizhh2009/article/details/6934845
这个不是重点:看不懂这个pom.xml没关系,也就是下载依赖包,打包,先继续往下看
@Path表示开启访问这个资源的路径
配置响应的listener和servlet无非就是初始resteasy的服务(先简单理解)
基于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- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>rest.resteasy</groupId>
- <artifactId>rest-resteay-demo</artifactId>
- <packaging>war</packaging>
- <version>1.0</version>
- <name>rest-resteay-demo Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <repositories>
- <repository>
- <id>java.net</id>
- <url>http://download.java.net/maven/1</url>
- <layout>legacy</layout>
- </repository>
- <repository>
- <id>maven repo</id>
- <name>maven repo</name>
- <url>http://repo1.maven.org/maven2/</url>
- </repository>
- <!-- For resteasy -->
- <repository>
- <id>jboss</id>
- <name>jboss repo</name>
- <url>http://repository.jboss.org/nexus/content/groups/public/</url>
- </repository>
- </repositories>
- <dependencies>
- <dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-jaxrs</artifactId>
- <version>2.2.1.GA</version>
- <!-- filter out unwanted jars -->
- <exclusions>
- <exclusion>
- <groupId>commons-httpclient</groupId>
- <artifactId>commons-httpclient</artifactId>
- </exclusion>
- <exclusion>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- </exclusion>
- <exclusion>
- <groupId>javax.xml.bind</groupId>
- <artifactId>jaxb-api</artifactId>
- </exclusion>
- <exclusion>
- <groupId>com.sun.xml.bind</groupId>
- <artifactId>jaxb-impl</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-jettison-provider</artifactId>
- <version>2.2.1.GA</version>
- <exclusions>
- <exclusion>
- <groupId>javax.xml.bind</groupId>
- <artifactId>jaxb-api</artifactId>
- </exclusion>
- <exclusion>
- <groupId>com.sun.xml.bind</groupId>
- <artifactId>jaxb-impl</artifactId>
- </exclusion>
- <exclusion>
- <groupId>javax.xml.stream</groupId>
- <artifactId>stax-api</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <finalName>rest-resteay-demo</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
2.编写jax-rs的服务类
- package resteasy.server;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.PathParam;
- @Path(value = "echo")
- public class Echo {
- @GET
- @Path(value = "/{message}")
- public String echoService(@PathParam("message") String message)
- {
- return message;
- }
- }
@GET表示响应HTTP 的get方法
@PathParam表示引用URI中得参数
详细的注解可参考我下面的参考文档
3.web.xml的配置
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
- <web-app>
- <context-param>
- <param-name>resteasy.resources</param-name>
- <param-value>resteasy.server.Echo</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
- </listener-class>
- </listener>
- <servlet>
- <servlet-name>Resteasy</servlet-name>
- <servlet-class>
- org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Resteasy</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- </web-app>
3.打包部署到响应的servlet容器即可(如tomcat),然后访问http://localhost:8080/rest-resteay-demo/echo/hello,world,网页上出现hello,world则成功
hello,world可换成任意字符,同样也将返回响应的字符
注:如果不使用maven,则可以到resteasy官网下载响应jar包即可
下一章预告:阐述不同的方式用resteasy发布我们的restful webservice 服务,有问题可跟帖,一起讨论,共同进步
参考文献: