users@jersey.java.net

Re: [Jersey] Variable changes do not happen

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 07 Jun 2010 09:22:13 +0200

Hi,

By default instances of root resource classes are created per request.
If you annotate with @Singleton there will an instance created per
application.

https://jersey.dev.java.net/nonav/documentation/latest/user-
guide.html#d4e464

Paul.

On Jun 6, 2010, at 3:21 PM, Rick Wager wrote:

>
> Hello,
>
> I believe you are getting a new, distinct instance of your resource
> each time you invoke the REST API. So, your constructor executes,
> your setter works fine - on that instance - and then when you do the
> subsequent get another instance is created and the value returned
> reflects the value set by the constructor.
>
> You could rearrange your code to have a static member (and not set
> the value in the constructor) to see it work the way you expect.
>
> Rick
>
> On Jun 6, 2010, at 6:53 AM, Muhra Daniel
> <daniel.muhra_at_googlemail.com> wrote:
>
>> Hi,
>>
>> I'm pretty new to all REST and Jersey stuff, but I'm already
>> playing around with it. So far, I got it all working, but one
>> problem is driving me nuts:
>>
>> Assume having a resource, which as a String member variable. Now I
>> have one method to change it and another one to get the current
>> value.
>> If I first change the value, it seems, like it's working, but when
>> I check it again using the get method, it still gives me the old
>> value? Why is that the case? And how could I resolve it.
>> Btw. It seems, that creating new Objects works fine (e.g. adding
>> new Objects to a List), but simple value changing seems to be
>> discarded.
>>
>> Below is a piece of the example I described:
>>
>> @Path("/string")
>> class StringTester{
>>
>> private String info;
>>
>> public StringTester(){
>>
>> info = "Foo";
>> }
>>
>> @GET
>> @Path("/set/{value}")
>> @Produces(MediaType.TEXT__PLAIN)
>> public String setString( @PathParam("value") String value ){
>> this.info = value;
>> return this.info;
>> }
>>
>> @GET
>> @Path("/get")
>> @Produces(MediaType.TEXT__PLAIN)
>> public String getString(){
>> return this.info;
>> }
>> }
>>
>> Cheers,
>> Nostradani