users@jersey.java.net

Updating an Object

From: Prat <enzo.660_at_gmail.com>
Date: Fri, 11 Apr 2008 11:44:41 -0700

Hello,
 I have just begun playing with RESTful webservices. Following is a simple
JSR311 annotated POJO.
--------------------------------------------------------------------------------------------------------------------------------------------------
@Path("/test")
public class TestResource {

private String message = new String("hello");

@Path("/setmessage")
@PUT
@ConsumeMime("text/plain")

public void setMessage(String message){
this.message = message;

}

@Path("/getmessage")
@GET
@ProduceMime("text/plain")
public String getMessage(){
return this.message;
}

}
--------------------------------------------------------------------------------------------------------------------------------------------------

I have written a client which tries the GET and PUT operations.
HttpResponse response;
response = makeHttpGetRequest("GET", "http://localhost:7456/test/getmessage",
"text/plain");
System.out.println(new Formatter().format("Returned Message:\n%s",
response.content.toString()));
//-- This prints "Returned Message: hello"

String newVal = "world";
InputStream inputStream = new ByteArrayInputStream(newVal.getBytes());
response = makeHttpRequest("PUT", "http://localhost:7456/test/getmessage",
"text/plain", inputStream);
//-- This should set the field 'message' to string 'world'.

response = makeHttpGetRequest("GET", "http://localhost:7456/test/getmessage",
"text/plain");
System.out.println(new Formatter().format("Returned Message:\n%s",
response.content.toString()));
//-- This prints "Returned Message: hello"

How do I set the field so that the consequent GET operation returns the
newly set value 'world' instead of the original value 'hello' without
annotating the resource with @Singleton(it works if it is made singleton or
if the field 'message' is static)?


-RP