简体   繁体   English

未找到响应类 ArrayList 的消息正文编写器

[英]No message body writer has been found for response class ArrayList

While I am trying to return List its throwing No message body writer has been found for response class ArrayList.当我尝试返回 List 时,其抛出没有找到响应类 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 . 要返回一个列表,最好将其包装到一个注释为@XmlRootElement的容器中,并将该列表作为一个字段给予该容器,注释为@XmlElement

Like so: 像这样:

@XmlRootElement
public class Container {
    @XmlElement
    public List yourlist;
}

看到这个 ,它的JAXB给你带来了问题,它不知道如何解组/编组List。

I've solved it using JacksonJaxbJsonProvider. 我用JacksonJaxbJsonProvider解决了它。 No Java code needs to be modified. 不需要修改Java代码。 Just few changes in Spring context.xml and Maven pom.xml , see https://stackoverflow.com/a/30777172/1245231 Spring context.xml和Maven pom.xml只有少量更改,请参阅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. 我已将List添加到域层的项目范围的现有对象中。

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. 它对项目更具上下文性,并且开箱即用:不需要测试XmlRootElement,而是为该对象的现有测试用例列表添加测试数据+逻辑。

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.在同一个项目中同时拥有 org.codehaus.jackson 和 com.fasterxml.jackson 依赖项将导致此 No message body writer 问题,无论注释如何。

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在我的例子中,Bean 由 com.fasterxml.jackson -> jackson-jaxrs-json-provider 编组,由 org.codehaus.jackson -> jackson-jaxrs 解组

So removing / updating all the references from org.codehaus.jackson to com.fasterxml.jackson fixed this issue.因此,删除/更新从 org.codehaus.jackson 到 com.fasterxml.jackson 的所有引用解决了这个问题。

I have updated it in cxf-servlet.xml, pom.xml and in all java class imports.我已经在 cxf-servlet.xml、pom.xml 和所有 java 类导入中更新了它。

pom.xml 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 cxf-servlet.xml 中的提供者

<jaxrs:providers>
   <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
</jaxrs:providers>

尝试使用GenericEntity。

Response.ok(new GenericEntity<List<String>>(yourCollectionOfStrings) {}).build();

Add this Maven dependency: 添加此Maven依赖项:

<init-param>
    <param-name>jaxrs.providers</param-name>
    <param-value>org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider</param-value>
</init-param>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到响应类ArrayList [jboss 7]的消息正文编写器 - No message body writer has been found for response class ArrayList [jboss 7] 在restfulcxf中未找到响应的消息正文编写器 - No message body writer has been found for response in restfulcxf 访问服务时,未找到响应类别的消息正文编写器 - No message body writer has been found for response class when accessing the service Apache CXF作为Restful API的客户端-找不到该类的消息正文编写器 - Apache CXF as Client for restful API - No message body writer has been found for class 严重:未找到 class java.util.Vector 的消息正文编写器,ContentType:application/xml - SEVERE: No message body writer has been found for class java.util.Vector, ContentType: application/xml 找不到Java类的消息正文编写器 - A message body writer for Java class not found 没有为MultipartBody,multipart / form-data找到消息正文编写器 - No message body writer has been found for MultipartBody, multipart/form-data 在osgi中使用带有cxf的jackson提供程序(未找到消息正文编写器) - Using jackson provider with cxf in osgi (no message body writer has been found) 找不到Java类型类java.util.ArrayList和MIME媒体类型application / xml的消息正文编写器 - A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found 找不到Java类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM