Justas Justas - 4 months ago 17
JSON Question

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

I have Jersey 1.19 REST web service which return custom @XmlRootElement response objects and works well. However, when I execute tests which should assert javax.ws.rs.core.Response later, I get exception:

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)


Response type:

@XmlRootElement
public class MyResponse {
public String message;
}


Endpoint which returns MyResponse:

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


My pom.xml fragments containing related libraries:

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


How to solve this problem?

Answer

It was the problem on the client side. Changed expected type from Response to 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);
}
Comments