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中,如下设置:
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">
Restful Web Application




resteasy.resources
com.liao.MessageRestService



resteasy.servlet.mapping.prefix
/rest




org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap




resteasy-servlet

org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher




resteasy-servlet
/rest/*




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

  
resteasy.scan
true

   则其实会出错,说:
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,只能手工注册,比如:
  
resteasy.resources
com.mkyong.rest.MessageRestService

    即可
 

你可能感兴趣的:(JAVA)