Dec 07

There is a problem with the embedded Tomcat server used by GWT in Hosted Mode resulting in the status code in the response always being 200, and the location reference being null.

To workaround this bug you need to add a dummy entity to the response on the server side. This ensures the correct status code and location are received by the client.

On the server side:


getResponse().setStatus(Status.SUCCESS_CREATED, "Widget added");
getResponse().setLocationRef("/widgets/"+widget.getId());

// add the following workaround
getResponse().setEntity(
   new StringRepresentation(
      "http://helpdesk.objects.com.au/java/how-to-get-the-correct-status-and-location-from-response-when-using-restlet-and-gwt-in-hosted-mode"));

written by objects \\ tags: , , , ,

Nov 10

Am using the following code in my GWT client application to get the string values from JSON data returned from the server. Problem is that the quotes are included in the returned string value.
How do I extract just the strings (without the quotes)?


// JSON data looks like:
// { "1": "Ben 10", "2": "Homer Simpson" }

for (String key : jsonobject.keySet())
{
   JSONValue value = jsonobject.get(key);
   System.out.println("["+value+"]");
}

// This is returning:
// ["Ben 10"]
// ["Homer Simpson"]

Problem is that the code (implicitly) uses the toString() method of JSONValue to get a string representation of the value. What you should be doing is using the isString() to both check it is a string, and return a JSONString instance. You can then use it’s stringValue() method to get the value of the string (without the quotes).

Here’s the modified code:


// JSON data looks like:
// { "1": "Ben 10", "2": "Homer Simpson" }

for (String key : jsonobject.keySet())
{
   JSONValue jsonvalue = sonobject.get(key);
   String value = jsonvalue.isString().stringValue();
   System.out.println("["+value+"]");
}

// This is returning:
// [Ben 10]
// [Homer Simpson]

written by objects \\ tags: ,