resteasy的一些配置
总结:
增加rest服务
方式一:
自动增加rest服务
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
方式二:
手动增加rest服务
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mkyong.app.MessageApplication</param-value>
</init-param>
</servlet>
package com.mkyong.app;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import com.mkyong.rest.MessageRestService;
public class MessageApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public MessageApplication() {
singletons.add(new MessageRestService());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
MessageApplication这个中用@ApplicationPath("/api")设置基路径,刚好是前缀还有用吗
示例:
修改了原文部分错误(web.xml文件中和第二种方法)
RestEasy, Jboss项目的JAX-RS参考实现。
1. 技术和工具:
RESTEasy 2.2.1.GA
JDK 1.6
Maven 3.0.3
Eclipse 3.6
2. 目录结构
3. 标准的Web项目
创建一个标准的Maven web 项目结构(可以通过eclipse中选择一个web模版项目来创建)
mvn archetype:create -DgroupId=com.mkyong.common -DartifactId=RESTfulExample
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
转换为Eclipse web工程:
mvn eclipse:eclipse -Dwtpversion=3.2.5
4. 项目依赖
在pom.xml中声明Jboss 依赖为仓库为 resteasy-jaxrs(如果有私有仓库如nexus,当然这个可以不用)
<project ...">
<!-- 有私有仓库这个就不用写啦 -->
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>
</dependencies>
</project>
5. 写个REST服务类
package com.mkyong.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/message")
public class MessageRestService {
public MessageRestService(){}
@GET
@Path("/{param}")
public Response printMessage(@PathParam("param") String msg) {
String result = "Restful example : " + msg;
return Response.status(200).entity(result).build();
}
}
6. web.xml
现在配置监听器和servlet以支持RESTEasy,(详细参考JBOSS documentation)
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<!-- 这句是关键 -->
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.mkyong.rest.MessageRestService</param-value>
</context-param>
<!-- this need same with resteasy servlet url-pattern -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
注:在上面的例子中, resteasy servlet url-pattern是"/rest/*",因此必须设置“resteasy.servlet.mapping.prefix”为“/rest”。
注意设置“resteasy.scan”为true,以便RESTEasy扫描和自动注册你的REST服务。
7. 浏览器地址访问目录结构为:“工程URL/rest/message/”匹配“MessageRestService”,并且“工程URL/rest/message/{任意值}”匹配@PathParam参数
如:http://localhost:8080/RESTfulExample/rest/message/aaa,
结果为:Restful example : aaa
8. 上面的例子是通过"ResteasyBootstrap"监听的。下面是另外一种方式。
创建一个类并且继承javax.ws.rs.core.Application,并且手动增加REST服务。
package com.mkyong.app;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import com.mkyong.rest.MessageRestService;
public class MessageApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public MessageApplication() {
singletons.add(new MessageRestService());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
web.xml中不再监听,修改web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<!-- 这句是关键 -->
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.mkyong.rest.MessageRestService</param-value>
</context-param>
<!-- this need same with resteasy servlet url-pattern -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mkyong.app.MessageApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
上一篇: RestEsay简介
下一篇: HornetQ与JBoss4集成