Genson with Jersey JsonBindingException: Failed to deserialize to type java.lang.String class

In my Spring MVC Java application, I am using Jersey REST API. I am using Jackson to process JSON. I added Genson to the project to filter some fields for some classes. And I'm trying to use Genson just for that, and I'll leave I want to use Jackson.

But I am getting an exception when using the Jersey REST api as Jersey has enabled Genson by default when the Jar is in the classpath ( Understand to learn from this link ).

Here is my code in connecting to a web service and getting data and converting to String using Jackson

public String connect() throws Exception {
        Client client = Client.create();
        try {
            String auth = new String(Base64.encode(userName + COLON + password));
            WebResource webResource = client
                    .resource(webServiceUrl);
            ClientResponse response = webResource
                    .header(AUTHORIZATION, BASIC + auth).type(APPLICATION_JSON)
                    .accept(APPLICATION_JSON).get(ClientResponse.class);
            //code to check response status code.
                stringResponse = response.getEntity(String.class);

        } catch (ClientHandlerException e) {
            throw new Exception("Not a Valid Url.");
        } finally {
            client.destroy();
        }
        return stringResponse;
}

      

I am getting the following exception

javax.ws.rs.WebApplicationException: com.owlike.genson.JsonBindingException: Could not deserialize to type class java.lang.String
    at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:130)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)
    at com.myapp.WebHttpClient.connect(WebHttpClient.java:74)
    at com.myapp.connection.connectors.WebConnector.getData(WebConnector.java:39)
    at com.myapp.adapter.impl.SourceAdapter.getData(SourceAdapter.java:29)
    at com.myapp.service.impl.WebServiceImpl.adapter(WebServiceImpl.java:174)
    at com.myapp.service.impl.WebServiceImpl.connect(WebServiceImpl.java:107)
    ... 41 more
Caused by: com.owlike.genson.JsonBindingException: Could not deserialize to type class java.lang.String
    at com.owlike.genson.Genson.deserialize(Genson.java:398)
    at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:128)
    ... 48 more
Caused by: com.owlike.genson.stream.JsonStreamException: Readen value can not be converted to String
    at com.owlike.genson.stream.JsonReader.valueAsString(JsonReader.java:200)
    at com.owlike.genson.convert.DefaultConverters$StringConverter.deserialize(DefaultConverters.java:364)
    at com.owlike.genson.convert.DefaultConverters$StringConverter.deserialize(DefaultConverters.java:351)
    at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:56)
    at com.owlike.genson.Genson.deserialize(Genson.java:396)
    ... 49 more

      

Line 74 refers to stringResponse = response.getEntity(String.class);

.

How do I disable Genson for Jersey or How do I fix this issue with Genson?

thank

0


source to share


1 answer


The current Genson documentation is hosted on github http://owlike.github.io/genson/ .

What's really going on

I'm not clear where you want to use Genson and how. Are you using Genson for serialization only? Do you use it through a jersey or directly?

When you get getEntity (String.class) Jersey asks Genson to discard the incoming json to the string. Note that in general json format does not allow strings as root values.

Solutions



  • Update jersey 2.X version (and latest Genson), in newer version when you ask for input as string it doesn't pass MessageBodyReader (correct behavior IMO). Then you can use that line to deser for whatever type you want.

  • Use the getEntityInputStream method of the ClientResponse to get the original InputStream and convert it to a string.

  • Another solution would be to override the Gensons implementation of MessageBodyReader so as not to handle the descriptor in String.


Having multiple libraries that handle JSON transformation at the same time is not right for me. Moreover, both (Jackson and Genson) provide some similar functionality. I have opened an issue allowing people to disable Genson in JAX-RS applications.

Upgrade to Genson 1.3 and above provide the system to disable Genson in the Jax-RS app .

+3


source







All Articles