com.owlike.genson.JsonBindingException:无法反序列化以输入类javax.ws.rs.core.Response

时间:2016-07-27 08:17:24

标签: java json web-services maven jersey

我有Jersey 1.19 REST Web服务,它返回自定义@XmlRootElement响应对象并且运行良好。但是,当我执行稍后应该断言javax.ws.rs.core.Response的测试时,我会得到异常:

javax.ws.rs.WebApplicationException: com.owlike.genson.JsonBindingException: Could not deserialize to type class javax.ws.rs.core.Response
    at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:127)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634)

回复类型:

@XmlRootElement
public class MyResponse {
    public String message;
}

返回MyResponse的端点:

@GET
@Path("/info")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public MyResponse getInfo(){
    return new MyResponse();
}

我的pom.xml片段包含相关的库:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.2.4</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>com.owlike</groupId>
    <artifactId>genson</artifactId>
    <version>1.4</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.1.3</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.19</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey.jersey-test-framework</groupId>
    <artifactId>jersey-test-framework-grizzly2</artifactId>
    <version>1.19</version>
    <scope>test</scope>
</dependency>

如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

这是客户端的问题。将预期类型从Response更改为com.sun.jersey.api.client.ClientResponse:

protected ClientResponse executeGet(String path){
    WebResource resource = resource().path(path);
    Builder builder = resource.header("Content-Type", "application/json;charset=UTF-8");
    return builder.get(ClientResponse.class);
}