application / xml没有MessageBodyWriter

时间:2016-06-02 13:33:11

标签: xml list resteasy

我正在使用Resteasy,只想从我的资源中返回一个字符串列表。它适用于JSON,但对于XML,我有错误Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/xml

我该怎么办?我在网上试过可能的选项,但没有任何效果。

@GET
@Path("")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Wrapped(element = "resources")
public Response getResources() {

  List<String> resources = ts.getAllResources();
  //GenericEntity<List<String>> entity = new GenericEntity<List<String>>(resources) {};

  return Response.ok().entity(new GenericEntity<List<String>>(resources) {}).build();
  //return Response.ok(resources).build();
  //return resources;
}

从pom.xml中提取

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>${jboss.resteasy.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-servlet-initializer</artifactId>
        <version>${jboss.resteasy.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>${jboss.resteasy.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-atom-provider</artifactId>
        <version>${jboss.resteasy.version}</version>
    </dependency>

更新 我注意到在Wildfly 10中部署时也出现以下错误。

WFLYSRV0059: Class Path entry jaxb-api.jar in /D:/Dev/Servers/wildfly-10.0.0.Final/standalone/deployments/service.war/WEB-INF/lib/jaxb-core-2.2.7.jar  does not point to a valid jar for a Class-Path reference.

1 个答案:

答案 0 :(得分:0)

由于我没有设法直接使用它,我创建了一个解决方法,意味着创建一个封装列表的类:

@XmlRootElement(name = "resources")
public static class ResourcesListSerializerHelper {
  @XmlElement(name = "resource")
  @JsonProperty(value = "resources")
  public List<String> resources = new ArrayList<String>();

  public ResourcesListSerializerHelper() {
  }

  public ResourcesListSerializerHelper(List<String> list) {
      resources.addAll(list);
  }
}

@GET
@Path("")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getResources() {
  List<String> resources = ts.getAllResources();
  return Response.ok().entity(new ResourcesListSerializerHelper(resources)).build();
}