问题描述:

While I am trying to return List its throwing No message body writer has been found for response class ArrayList.

I have code as follows:

@POST

@Path("/{scope}/{application}/tables")

@Produces("application/xml")

public List<String> getTableNames(@PathParam("scope") String scope,

@PathParam("application") String application, Request request) {

// For example, I am returning a list of String

return new ArrayList<String>(4);

}

Please help me. Thanks in advance

网友答案:

To return a list, best wrap it into a container annotated @XmlRootElement and give that container your list as a field, annotated as @XmlElement.

Like so:

@XmlRootElement
public class Container {
    @XmlElement
    public List yourlist;
}
网友答案:

See this, Its JAXB thats giving you problems, it doesn't know how to unmarshal/marshal a List.

网友答案:

To avoid the wrapping, one can use Jackson. On how to do it, you can follow my answer for a similar question.

网友答案:

I've solved it using JacksonJaxbJsonProvider. No Java code needs to be modified. Just few changes in Spring context.xml and Maven pom.xml, see http://stackoverflow.com/a/30777172/1245231

网友答案:

I have added the List to existing object of the project scope of domain layer.

It was more contextual for project and also worked out-of-the box: no needed to test XmlRootElement, but add the test data+logic for List of existing test case for that object.

网友答案:

Try using the GenericEntity.

Response.ok(new GenericEntity<List<String>>(yourCollectionOfStrings) {}).build();
相关阅读:
Top