1

I don't understand why when I receive a json encoded string this is not decoded automatically. I tried this code using Apache CFX 3.1.4 or Jersey 2.22.1:

Client client = ClientBuilder.newClient().register(JSONProvider.class);

WebTarget target = client.target("http://example.org/rest/service1");
target = target.queryParam("method", "method1");

Entity<EndpointRequest> entity = Entity.entity(new EndpointRequest("0000"),
                       MediaType.APPLICATION_JSON);
Response response = target.request()
                    .accept(MediaType.APPLICATION_JSON)
                    .post(entity);

System.out.println( response.getStatus() );

if (response.getStatus() == 200) {
// The problem comes here
    String basePath = response.readEntity(String.class);
    System.out.println( basePath );
}

The request is successfully executed but basePath contains "\/opt\/local\/application\/rest\/" (backslash and double quotes included)

basePath should instead contain this: /opt/local/application/rest/

It seems to me, the json deserialization hasn't be triggered when it should.

freedev
  • 25,946
  • 8
  • 108
  • 125

2 Answers2

0

You created a Client and a WebTarget but never actually used them. You end up using Entity and Response instead. I'm not sure what library you're using, but I would think you would need to actually use Ciient and WebTarget to invoke JSONProvider.

Brian Pipa
  • 808
  • 9
  • 23
0

The problem was the server response. A server should not return only a quoted string, even if correctly encoded, because a string is not a valid JSON object.

Community
  • 1
  • 1
freedev
  • 25,946
  • 8
  • 108
  • 125