2012-05-25 80 views
0

我有一個Jetty服務器(8.1.3),我想通過使用Jersey(1.12)的REST服務公開一些POJO。我有資源映射到JSON的問題,我不明白爲什麼。在Jetty中使用Jersey映射到JSON

Jetty和新澤西州的servlet開始:

public class JettyUtil { 
    public static void start() throws Exception { 
    Server server = new Server(); 
    SelectChannelConnector connector = new SelectChannelConnector(); 
    connector.setPort(8999); 
    server.addConnector(connector); 

    HandlerList handlers = new HandlerList(); 
    handlers.setHandlers(new Handler[] { createJerseyServlet(), new DefaultHandler() }); 
    server.setHandler(handlers); 

    server.start(); 
    server.join(); 
    } 

    private static Handler createJerseyServlet() { 
    ServletContextHandler handler = new ServletContextHandler(); 
    handler.setContextPath("/game"); 
    SessionHandler sessionHandler = new SessionHandler(); 
    handler.setSessionHandler(sessionHandler); 
    ServletHolder holder = handler.addServlet("com.sun.jersey.spi.container.servlet.ServletContainer", "/*"); 
    Map<String,String> initParams = ImmutableMap.of(
      "com.sun.jersey.config.property.packages", "com.test.todo.rest", 
      "com.sun.jersey.api.json.POJOMappingFeature", "true"); 
    holder.setInitParameters(initParams); 
    return handler; 
    } 
} 

資源:

@XmlRootElement 
public class Todo { 
    private String summary; 
    public String getSummary() { return summary; } 
    public void setSummary(String summary) { this.summary = summary; } 
} 

的Webservice:

@Path("/todo") 
public class TodoResource { 
    @GET 
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
    public Todo getTodo() { 
    Todo todo = new Todo(); 
    todo.setSummary("This is my first todo"); 
    return todo; 
    } 
} 

測試:

ClientConfig config = new DefaultClientConfig(); 
Client client = Client.create(config); 
WebResource service = client.resource(getBaseURI());  
System.out.println(
    service.path("game").path("todo"). 
    accept(MediaType.APPLICATION_JSON).get(String.class)); 

輸出Jetty服務器:

May 24, 2012 4:21:34 PM com.sun.jersey.spi.container.ContainerResponse write 
SEVERE: A message body writer for Java class com.test.todo.rest.Todo, and Java type class com.test.todo.rest.Todo, and **MIME media type application/json was not found** 

May 24, 2012 4:21:34 PM com.sun.jersey.spi.container.ContainerResponse write 
SEVERE: The registered message body writers compatible with the MIME media type are: 
*/* -> 
com.sun.jersey.core.impl.provider.entity.FormProvider 
com.sun.jersey.core.impl.provider.entity.StringProvider 
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider 
com.sun.jersey.core.impl.provider.entity.FileProvider 
com.sun.jersey.core.impl.provider.entity.InputStreamProvider 
com.sun.jersey.core.impl.provider.entity.DataSourceProvider 
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General 
com.sun.jersey.core.impl.provider.entity.ReaderProvider 
com.sun.jersey.core.impl.provider.entity.DocumentProvider 
com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider 
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter 
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter 
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General 
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General 

May 24, 2012 4:21:34 PM com.sun.jersey.spi.container.ContainerResponse logException 
SEVERE: Mapped exception to response: 500 (Internal Server Error) 
javax.ws.rs.WebApplicationException: 
com.sun.jersey.api.MessageException: A message body writer for Java class com.test.todo.rest.Todo, and Java type class com.test.todo.rest.Todo, and MIME media type application/json was not found 
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.jvva:1451) 
... 

問:

我應如何添加 '應用/ JSON' 作爲使用碼頭澤西島的servlet支持的MIME格式?我認爲將com.sun.jersey.api.json.POJOMappingFeature設置爲true是個訣竅? (抓取XML工作正常,但我需要它與Json一起工作)

回答

0

將lib jersey-jackson添加到您的類路徑中。

+2

謝謝!將「jersey-json」添加到pom中就有竅門了! – perseger