No message body writer has been found for response class ArrayList

前端 未结 7 1504
北荒
北荒 2020-12-09 19:21

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 
@Pat         


        
相关标签:
7条回答
  • 2020-12-09 19:27

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

    0 讨论(0)
  • 2020-12-09 19:34

    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.

    0 讨论(0)
  • 2020-12-09 19:42

    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;
    }
    
    0 讨论(0)
  • 2020-12-09 19:48

    Add this Maven dependency:

    <init-param>
        <param-name>jaxrs.providers</param-name>
        <param-value>org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider</param-value>
    </init-param>
    
    0 讨论(0)
  • 2020-12-09 19:49

    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

    0 讨论(0)
  • 2020-12-09 19:52

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

    0 讨论(0)
提交回复
热议问题