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
  • 79,279
  • 19
  • 185
  • 195
aswininayak
  • 933
  • 5
  • 22
  • 39
  • 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 – petrsyn Jun 11 '15 at 09:51
  • See [this](https://stackoverflow.com/questions/1603404/using-jaxb-to-unmarshal-marshal-a-liststring), Its JAXB thats giving you problems, it doesn't know how to unmarshal/marshal a List. – Yazan Jaber Feb 16 '12 at 12:51

5 Answers5

21

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,791
  • 3
  • 35
  • 49
0

Try using the GenericEntity.

Response.ok(new GenericEntity<List<String>>(yourCollectionOfStrings) {}).build();
Peter De Winter
  • 1,183
  • 2
  • 15
  • 27
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,458
  • 2
  • 27
  • 43
0

Having both org.codehaus.jackson and com.fasterxml.jackson dependencies in a same project will cause this No message body writer issue irrespective of annotations.

In my case, the Bean was marshal'd by com.fasterxml.jackson -> jackson-jaxrs-json-provider and unmarshall'd by org.codehaus.jackson -> jackson-jaxrs

So removing / updating all the references from org.codehaus.jackson to com.fasterxml.jackson fixed this issue.

I have updated it in cxf-servlet.xml, pom.xml and in all java class imports.

pom.xml

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-extension-providers</artifactId>
    <version>3.5.2</version>
</dependency>

<dependency>
     <groupId>com.fasterxml.jackson.jaxrs</groupId>
     <artifactId>jackson-jaxrs-json-provider</artifactId>
     <version>2.13.4</version>
</dependency>

Provider in cxf-servlet.xml

<jaxrs:providers>
   <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
</jaxrs:providers>
vsriram92
  • 593
  • 1
  • 4
  • 15
-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
  • 36,322
  • 27
  • 84
  • 93
syam
  • 1