13

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

Perception
  • 77,470
  • 19
  • 176
  • 187
aswininayak
  • 912
  • 3
  • 22
  • 39

7 Answers7

20

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;
}
Urs Reupke
  • 6,545
  • 3
  • 33
  • 48
4

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

Community
  • 1
  • 1
Yazan Jaber
  • 1,940
  • 22
  • 35
1

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

Community
  • 1
  • 1
petrsyn
  • 4,894
  • 3
  • 42
  • 47
0

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

Community
  • 1
  • 1
Dudi
  • 2,095
  • 1
  • 23
  • 37
  • 1
    You need that configuration if you intend to return the response as JSON. In fact if you look at this answer for this question (http://stackoverflow.com/questions/24653329/jaxrs-client-cont-find-message-body-writer/25536584#25536584) you will notice that simple configuration like this will work. But he wants a XML as a response. – saibharath Oct 15 '14 at 15:54
0

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.

Oleksii Kyslytsyn
  • 2,288
  • 2
  • 25
  • 42
-1

Try using the GenericEntity.

Response.ok(new GenericEntity<List<String>>(yourCollectionOfStrings) {}).build();
Peter De Winter
  • 1,143
  • 2
  • 15
  • 26
-1

Add this Maven dependency:

<init-param>
    <param-name>jaxrs.providers</param-name>
    <param-value>org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider</param-value>
</init-param>
Paul Roub
  • 35,848
  • 27
  • 79
  • 88
syam
  • 1