java - com.owlike.genson.JsonBindingException : Could not deserialize to type class javax. ws.rs.core.Response

标签 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>

如何解决这个问题?

最佳答案

这是客户端的问题。将预期类型从 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/

相关文章:

arrays - Polymer 1.0 将 JSON 字符串作为属性传递

java - 修改/更新 jsonArray 中的值?

c# - 如何将日期时间参数提交给 WCF REST 服务

javascript - 在网页上显示 xml 信息的最佳方式是什么?

iphone - 在 Web 服务之前加载 UITableViewController

java - SwingWorker(线程)永远不会死(到达 "return null;"后仍然存在)?

java - 使用Struts 2上传图像

java - LLVM:无法让 vmjc 工作

java - Java 中的命令行查询

c# - 在 JSON.NET 中如何获取对每个反序列化对象的引用?