No message body writer has been found for response class ArrayList
Asked Answered
R

5

14

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

Raab answered 16/2, 2012 at 12:37 Comment(2)
See this, Its JAXB thats giving you problems, it doesn't know how to unmarshal/marshal a List<String>.Fence
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://mcmap.net/q/282221/-using-jaxb-to-unmarshal-marshal-a-list-lt-string-gtContemporary
B
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;
}
Butter answered 16/2, 2012 at 12:57 Comment(1)
And make sure that Container has a default, no-args constructor :)Ambulate
W
0

Try using the GenericEntity.

Response.ok(new GenericEntity<List<String>>(yourCollectionOfStrings) {}).build();
Westward answered 13/11, 2013 at 20:36 Comment(0)
A
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.

Allegraallegretto answered 23/3, 2016 at 12:30 Comment(0)
B
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>
Boyish answered 23/12, 2022 at 18:3 Comment(0)
A
-1

Add this Maven dependency:

<init-param>
    <param-name>jaxrs.providers</param-name>
    <param-value>org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider</param-value>
</init-param>
Abb answered 4/10, 2018 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.