JBoss Resteasy初探

JBoss Resteasy是一个遵守JAX-RS 标准的REST的开源项目,下载地址在:
http://www.jboss.org/resteasy,下载后,将LIB包下的所有文件放好后,
先来写个HELLO WORLD,注意要用JDK 1。6

package com.liao;

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 {

@GET
@Path("/{param}")
public Response printMessage(@PathParam("param") String msg) {

String result = "Restful example : " + msg;

return Response.status(200).entity(result).build();

}

}

然后在WEB中,如下设置:
<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.liao.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>

这里,我们期望使用
http://localhost/ReseteasyHelloWorld/rest/message/hello
来访问,所以url-pattern里设置为/rest/*,并且注意
一定要有<param-name>resteasy.servlet.mapping.prefix</param-name>
,还要注意的事,如果用了

<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
则其实会出错,说:
SEVERE: Exception sending context initialized event to listener instance of class
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
java.lang.RuntimeException: Unable to instantiate MessageBodyReader
at org.jboss.resteasy.spi.ResteasyDeployment.registerProvider(ResteasyDeployment.java:505)
at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:305)
at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:225)
//...
Caused by: java.lang.RuntimeException:
Illegal to inject a message body into a singleton into public
org.codehaus.jackson.jaxrs.JacksonJsonProvider(
org.codehaus.jackson.map.ObjectMapper,org.codehaus.jackson.jaxrs.Annotations[])
... 20 more

估计是现在的BUG,只能手工注册,比如:
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.mkyong.rest.MessageRestService</param-value>
</context-param>
即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值