您的位置:首页 > 运维架构 > 网站架构

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

2014-08-02 16:41 627 查看
转载请标明出处: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

[html]
view plaincopy

<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>

这个不是重点:看不懂这个pom.xml没关系,也就是下载依赖包,打包,先继续往下看
2.编写jax-rs的服务类

[java]
view plaincopy

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;
}
}

@Path表示开启访问这个资源的路径

@GET表示响应HTTP 的get方法

@PathParam表示引用URI中得参数

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

3.web.xml的配置

[html]
view plaincopy

<!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> <!--个人理解是设定resteasy的资源目录 -->
<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 <!--个人理解是监听resteasyboot启动,我想就是resteasy这个servlet初始化的时候吧 -->
</listener-class>
</listener>

<servlet>
<servlet-name>Resteasy</servlet-name> <!--个人理解是配置resteasy核心,映射相应路径交给resteasy处理-->
<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>

配置响应的listener和servlet无非就是初始resteasy的服务(先简单理解)
3.打包部署到响应的servlet容器即可(如tomcat),然后访问http://localhost:8080/rest-resteay-demo/echo/hello,world,网页上出现hello,world则成功

hello,world可换成任意字符,同样也将返回响应的字符

注:如果不使用maven,则可以到resteasy官网下载响应jar包即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐