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.
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 https://stackoverflow.com/a/30777172/1245231
To avoid the wrapping, one can use Jackson. On how to do it, you can follow my answer for a similar question.
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();
Add this Maven dependency:
<init-param>
<param-name>jaxrs.providers</param-name>
<param-value>org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider</param-value>
</init-param>
来源:https://stackoverflow.com/questions/9311396/no-message-body-writer-has-been-found-for-response-class-arraylist