找不到Java类java.util.LinkedList的消息正文编写器

时间:2013-07-10 12:02:26

标签: rest jersey

我在球衣网络服务中有这个代码:

@Path("/areThereNewOrders")
    @GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON,
            MediaType.TEXT_XML, MediaType.TEXT_HTML })
    public List<Integer> areThereNewOrders() {
        if (NewOrders.newOrders != null
                && NewOrders.newOrders.containsKey(restaurantID)
                && NewOrders.newOrders.get(restaurantID).size() != 0) {
            return NewOrders.newOrders.get(restaurantID);
        } else {
            System.out.println("No New Orders For Restaurant " + restaurantID);
            List<Integer> r = new LinkedList<Integer>();
            return r;
        }
    }

但我得到了这个例外:

Jul 10, 2013 2:59:22 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class java.util.LinkedList, and Java type java.util.List<java.lang.Integer>, and MIME media type text/html was not found
Jul 10, 2013 2:59:22 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.json.impl.provider.entity.JSONJAXBElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONWithPaddingProvider
  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
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy

Jul 10, 2013 2:59:22 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 java.util.LinkedList, and Java type java.util.List<java.lang.Integer>, and MIME media type text/html was not found
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1479)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.LinkedList, and Java type java.util.List<java.lang.Integer>, and MIME media type text/html was not found
    ... 25 more

我确定错误,因为我返回一个整数列表,但我不知道解决方案。

任何帮助都会有所帮助,在此先感谢

2 个答案:

答案 0 :(得分:1)

返回

List<Integer> 

List<String> 

没有为JAXB提供足够的信息来编组XML或JSON。 您通常希望返回一个域对象,其中包含的信息不仅仅是ids。

因此:

public List<Integer> areThereNewOrders() {

应该改为

public List<Order> areThereNewOrders() {

@XmlRootElement// this gives the root element the name order
public class Order {
    private Integer id;
    private String name;
..
}

这允许JAXB marshaller转换为

{"order":[{"id":"1","name":"Order 1"},{"id":"2","name":"Order 2"}]}

从Json解组名称&#34; order&#34;使用。

答案 1 :(得分:0)

尝试创建一个带JAXB注释的包装器,例如:

@XmlRootElement(name="list")
public class JaxbList<T> implements List<T> {
    protected List<T> list;
    public JaxbList(List<T> list){
        this.list=list;
    }
    @XmlElement(name="item")
    public List<T> getList(){
        return this;
    }
}

并从Jersey方法返回

return new JaxbList<Integer>(r);