我有一个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; }
}
web服务:
@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)
...
问题:
如何使用Jetty中的Jersey servlet将'application / json'添加为支持的MIME格式?我认为将com.sun.jersey.api.json.POJOMappingFeature设置为true是诀窍吗? (获取XML工作正常,但我也需要它与Json一起工作)
答案 0 :(得分:0)
将lib jersey-jackson
添加到类路径中。