1
votes

I have this code in a jersey web service:

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

but I got this exception:

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

I am sure that the error because I return a list of integer but i don't know the solution.

any help would be helpful, thanks in advance

2
Have you tried Generics?public GenericEntity<List<Integer>> areThereNewOrders() - AlvinArulselvan
I will try it, please - Marco Dinatsoli
@AlvinArulselvan what should I return in the order of generic entity? - Marco Dinatsoli
@AlvinArulselvan there is no such constructor - Marco Dinatsoli
return new GenericEntity<List<String>>(r) Generics is in javax.ws.rs.core.GenericEntity - AlvinArulselvan

2 Answers

1
votes

Returning a

List<Integer> 

or a

List<String> 

does not give JAXB enough information to marshal the XML or JSON. You generally want to return a domain object with more information than just ids.

Therefore :

public List<Integer> areThereNewOrders() {

should change to

public List<Order> areThereNewOrders() {

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

This allows the JAXB marshaller to convert into

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

To unmarshal from Json the name "order" is used.

0
votes

Try to create a JAXB-annotated wrapper, e.g.:

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

and return that from the Jersey method

return new JaxbList<Integer>(r);