Search code examples
javajsonjerseygenson

Jersey client send string with application/json type


I need to send a String that is already in JSON format using the Jersey client 1.19 and genson 1.3

Client.create().resource(path).webResource.type(MediaType.APPLICATION_JSON_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE).put(ClientResponse.class, jsonAsString)

The problem with this is that the client is parsing the string, I need it to be sent as it is. I'm sending something like { "name":"Foo" } and the client is converting it to "{ \"name\":\"Foo\" }". If I change the type to PLAIN_TEXT it sends the request correctly but I need to send it as application/json .


Solution

  • So yes Genson will try to encode your string as a literal json string. In this case it is maybe not what you would want, but more generally it is what people would expect: serialize à java string as a json string.

    The solution I see is too extend GensonJsonConverter and override isWritable to return false when the input type is string. Then just register it. That should work.

    I've opened this issue so it can be added as a more flexible feature.