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

你可能感兴趣的:(exception,jboss,String,servlet,REST,import)