users@jersey.java.net

[Jersey] Re: version 1.18.1 rest & Integer

From: Michal Gajdos <michal.gajdos_at_oracle.com>
Date: Tue, 22 Jul 2014 10:58:18 +0200

Hi Tomaz,

even though a single value is not a valid json object (in your case a number), Jackson is capable of (un)marshaling this value. The better approach would be to wrap this value into a POJO.

Do you enable FEATURE_POJO_MAPPING on the client-side to make sure Jackson is (un)marshaling the entity?

final ClientConfig cc = new DefaultClientConfig();
cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
final Client client = Client.create(cc);

Michal

> On 22 Jul, 2014, at 08:36 , Tomaz Majerhold <tomaz.majerhold_at_arnes.si> wrote:
>
> Yes I know that, but shouldn't the anotation
>
> @Produces({"application/json"})
> public Integer getInt();
>
> do exectly that content-type header application/json
>
> Regards, Tomaz
>
> Dne 21.7.2014 16:59, piše Joe Mocker:
>> The exception says it all. Jersey is trying to read it from a text/HTML input but doesn't have any way to do that. I'm guess you really just forgot to add a content-type header that is application/json.
>>
>>> On Jul 21, 2014, at 7:13 AM, "Tomaz Majerhold" <tomaz.majerhold_at_arnes.si> wrote:
>>>
>>> I have problem to read REST Integer response with jersey client, but if I try with browser it display it correctly.
>>>
>>>
>>> 1) server side:
>>>
>>> maven:
>>> -----------
>>> <dependency>
>>> <groupId>com.sun.jersey</groupId>
>>> <artifactId>jersey-json</artifactId>
>>> <version>1.18.1</version>
>>> </dependency>
>>>
>>> rest:
>>> ----------
>>> @Path("/int")
>>> @GET
>>> @Produces({"application/json"})
>>> public Integer getInt();
>>>
>>> And in javax.ws.rs.core.Application I add JacksonJsonProvider.class
>>>
>>> 2) client side:
>>>
>>> maven:
>>> -----------
>>> <dependency>
>>> <groupId>com.sun.jersey</groupId>
>>> <artifactId>jersey-json</artifactId>
>>> <version>1.18.1</version>
>>> </dependency>
>>> <dependency>
>>> <groupId>com.sun.jersey</groupId>
>>> <artifactId>jersey-json</artifactId>
>>> <version>1.18.1</version>
>>> </dependency>
>>>
>>> rest:
>>> ----------
>>> ClientResponse clientResponse = webResource
>>> .path(PATH_BASIC+"/"+"int")
>>> .accept("application/json")
>>> .get(ClientResponse.class);
>>>
>>> testInt = clientResponse.getEntity(Integer.class);
>>>
>>> HTTP Header:
>>> ------------------
>>> GET /aai-api-server/rest/wc/int HTTP/1.1
>>> Accept: application/json
>>> Authorization: Basic Ymxhei5kaXZqYWtAYXJuZXMuc2k6Ymxhei5kaXZqYWtAYXJuZXMuc2k=
>>> Cache-Control: no-cache
>>> Pragma: no-cache
>>> User-Agent: Java/1.7.0_25
>>> Host: localhost:8181
>>> Connection: keep-alive
>>>
>>>
>>> Error:
>>> ----------
>>> com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class java.lang.Integer, and Java type class java.lang.Integer, and MIME media type text/html; charset=ISO-8859-1 was not found
>>>
>>>
>>> What I'm doing wrong?
>>>
>>> Regards, Tomaz
>