0

我有一个 Jetty 服务器(8.1.3),我想在其中使用 Jersey(1.12)通过 REST 服务公开一些 POJO。我在将资源映射到 JSON 时遇到问题,我不知道为什么。

Jetty 和 Jersey 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; }
}

网络服务:

@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));

输出码头服务器:

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)
...

问题:

如何使用 Jetty 中的 Jersey servlet 将“application/json”添加为受支持的 MIME 格式?我认为将 com.sun.jersey.api.json.POJOMappingFeature 设置为 true 是诀窍吗?(获取 XML 工作正常,但我也需要它与 Json 一起工作)

4

1 回答 1

0

将 lib 添加jersey-jackson到您的类路径中。

于 2012-05-25T07:51:17.590 回答