1

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

@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

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

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

4

2 回答 2

1

返回一个

List<Integer> 

或一个

List<String> 

没有为 JAXB 提供足够的信息来编组 XML 或 JSON。您通常希望返回包含更多信息的域对象,而不仅仅是 id。

所以 :

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 编组器转换为

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

为了从 Json 中解组,使用了名称“order”。

于 2014-05-28T13:58:15.297 回答
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);
于 2013-07-10T21:44:03.643 回答