找不到内容类型application / xml type

时间:2016-10-18 14:42:17

标签: java jaxb jersey

我正在按照这个解决方案尝试消除我的问题,但我的失败是悲惨的:RESTEasy: Could not find writer for content-type application/json type

以下是我的maven依赖项:

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>2.2.1.GA</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.9</version>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.12</version>
</dependency>

这是我创建的一个我编组/解组的XML类。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Cools")
public class CoolXml {

    @XmlAttribute
    private Boolean isCool;

    public Boolean getIsCool() {
        return isCool;
    }

    public void setIsCool(final Boolean isCool) {
        this.isCool = isCool;
    }
}

使用此代码调用端点时,最后一行会生成错误:

final String url = "http://localhost:8080/sample/getcools";
final ClientRequest request = new ClientRequest(url);
request.header(HttpHeaderNames.ACCEPT, MediaType.APPLICATION_XML);
final CoolXml req = new CoolXml(project);
request.body("application/xml", req);
final ClientResponse<CoolXmlResponse> response = request.post(CoolXmlResponse.class);

我收到了超级烦人的错误:

 java.lang.RuntimeException: could not find writer for content-type application/xml type: cool.CoolXml

我是否需要任何其他依赖项?

1 个答案:

答案 0 :(得分:0)

我最终使用了这种依赖:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.8</version>
</dependency>

然后,我最终使用以下类来测试请求:

final Client client = Client.create();
final WebResource webResource = client.resource(url);
final CoolXml requestXml = new CoolXml();
... some data setting
final ClientResponse response = webResource.accept("application/xml").post(ClientResponse.class, requestXml);