我有 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>
如何解决这个问题?
最佳答案
这是客户端的问题。将预期类型从 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);
}
关于java - com.owlike.genson.JsonBindingException : Could not deserialize to type class javax. ws.rs.core.Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38607490/