users@jersey.java.net

[Jersey] Re: Returned String value is not encoded

From: Martin Ždila <m.zdila_at_gmail.com>
Date: Tue, 28 Feb 2012 21:50:43 +0100

On Tue, Feb 28, 2012 at 9:21 PM, Petr Jurák <petr.jurak_at_gmail.com> wrote:
> Well, I think it's okay. It should return plain text because it's
> simple type (no bean). Similiar it's for Integer, Double etc.

It is a simple type, but why it isn't JSON encoded? Why it is handled
different way than complex objects? Integer and Double doesn't need to
be specially encoded, toString() is sufficient.

> Have a look at this example:
>
>    @GET
>    @Produces({MediaType.APPLICATION_JSON})
>    public Object getJson() {
>        Object person = new Object() {
>
>            private String name = "Petr";
>            private Integer age = 28;
>
>            public Integer getAge() {
>                return age;
>            }
>
>            public String getName() {
>                return name;
>            }
>        };
>        return person;
>    }
>
> This should return {"name":"Petr","age":28}, which is well-formatted JSON.

That's fine, this has been working for me too :-)

> I don't understand why you're returning String object, but you can
> achieve that like return "\"yourstring\"";

I need plain String because this is the type of the method result:
String getHostname().

BTW Returning "\"yourstring\"" is a workaround that ignores Accept
header and forces application/json.

Still thanks for your effort, I appreciate it even it doesn't solve my problem.

> On 28 February 2012 20:27, Martin Ždila <m.zdila_at_gmail.com> wrote:
>> On Tue, Feb 28, 2012 at 8:11 PM, Petr Jurák <petr.jurak_at_gmail.com> wrote:
>>> What version of Jersey are you using? I'll check something.
>>
>> It is 1.11
>>
>>> On 28 February 2012 20:00, Martin Ždila <m.zdila_at_gmail.com> wrote:
>>>> Unfortunately @Produces({ MediaType.APPLICATION_JSON }) doesn't help.
>>>> The result is still
>>>> Hello
>>>> instead of expected
>>>> "Hello"
>>>> .
>>>>
>>>> Could it be a bug? If so then bug in Jersey or in Jackson?
>>>>
>>>>
>>>> On Tue, Feb 28, 2012 at 7:36 PM, Petr Jurák <petr.jurak_at_gmail.com> wrote:
>>>>> Hi,
>>>>>
>>>>> imho he's just missing @Produces({MediaType.APPLICATION_JSON }).
>>>>>
>>>>> On 28 February 2012 19:33, Gonfi den Tschal <gonfidentschal_at_gmail.com> wrote:
>>>>>> a string is a string, also in json. it makes no sense to wrap that in
>>>>>> another object or formatting. it's not xml ;-)
>>>>>>
>>>>>> On Tue, Feb 28, 2012 at 8:16 PM, Martin Ždila <m.zdila_at_gmail.com> wrote:
>>>>>>> Hello
>>>>>>>
>>>>>>> I have method like this
>>>>>>>
>>>>>>> @GET
>>>>>>> public String getMessage() {
>>>>>>>    return "Hello";
>>>>>>> }
>>>>>>>
>>>>>>>
>>>>>>> If I call it with Accept: application/json, I get a plain string
>>>>>>> without quotes. How to get properly encoded JSON string? With complex
>>>>>>> objects it works. I am using Jersey with Jackson.
>>>>
>>>> --
>>>> Ing. Martin Ždila
>>>> tel:+421-908-363-848
>>>> mailto:martin_at_zdila.sk
>>>> http://www.zdila.sk/
>>
>>
>>
>> --
>> Ing. Martin Ždila
>> tel:+421-908-363-848
>> mailto:martin_at_zdila.sk
>> http://www.zdila.sk/



-- 
Ing. Martin Ždila
tel:+421-908-363-848
mailto:martin_at_zdila.sk
http://www.zdila.sk/