How to do GET, POST, PUT, DELETE

277 views
Skip to first unread message

Thiruppathy Ramachandran

unread,
Jan 1, 2014, 2:18:53 AM1/1/14
to rest...@googlegroups.com
Hi,

I'm trying to invoke the Rest Services. The services is in the different server from where the GWT application is deployed.

Restservice URLs:
localhost:8080/greeting
localhost:8080/testpost

GWT URL:

The GET method is working fine with the RestService implementation using the JSONP callback. Is the JSONP is mandatory.
The POST method is not working using the RESTYGWT.

Server side code:
    @RequestMapping(method=RequestMethod.GET, value="/greeting", headers = "Accept=application/json")
    public @ResponseBody String greeting(
            @RequestParam(value="name", required=false, defaultValue="World") String name,
            @RequestParam(value="jsonCallback", required=false) String callBack) {

System.out.println("Invoked with the name:"+name);
String returnValue = "";
if (callBack != null) {
returnValue = callBack + "(";
}
returnValue += serializeToJson(new Greeting(counter.incrementAndGet(), String.format(template, name)));

if (callBack != null) {
returnValue += ")";
}
return returnValue;
    }


    @RequestMapping(method=RequestMethod.POST, value="/testpost", headers="Accept=application/json")
    public @ResponseBody String testPost(
    @RequestBody Person person,
            @RequestParam(value="jsonCallback", required=false) String callBack) {

System.out.println("Invoked with the parameter:"+person.name);
String returnValue = "";
if (callBack != null) {
returnValue = callBack + "(";
}
returnValue += serializeToJson(new Greeting(counter.incrementAndGet(), "Hello "+person.name));

if (callBack != null) {
returnValue += ")";
}

return returnValue;
    }

    private String serializeToJson(Object theObject) {

    String theJsonString = null;

    try {
    ObjectMapper theObjectMapper = new ObjectMapper();
    ByteArrayOutputStream theJsonOutputStream = new ByteArrayOutputStream();
    theObjectMapper.writeValue(theJsonOutputStream, theObject);

    theJsonString = theJsonOutputStream.toString("UTF-8");
    } catch (Exception theException) {
           theException.printStackTrace();
            }

         return theJsonString;

    }

Client Side Code:
Using GET:
public interface TempService extends RestService {
    @JSONP(callbackParam="jsonCallback")
    public void order(MethodCallback<Response> callback);
}

      TempService service = GWT.create(TempService.class);
      service.order(new MethodCallback<Response>() {
public void onSuccess(Method method, Response response) {
Window.alert(response.toString());
}
public void onFailure(Method method, Throwable exception) {
Window.alert("failure");
}
});

The above code is working fine with the JSONP callback. But not without JSONP callback.

Using POST:
@Path("/testpost")
public interface Service1 extends RestService {
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void testPost(Person request, MethodCallback<Response> callback);

}

      Resource resource = new Resource("http://localhost:8080");
     Service1 service = GWT.create(Service1.class);
     ((RestServiceProxy)service).setResource(resource);
     Person person = new Person();
     person.id = 1;
     person.name = "a";
     service.testPost(person, new MethodCallback<Response>() {
public void onSuccess(Method method, Response response) {
Window.alert("success");
}
public void onFailure(Method method, Throwable exception) {
Window.alert("failure");
}
});

The above code is failing with FailedStatusCodeException.

Can you please explain how can I achieve calling the POST? 

Please help!

Thanks
Thiruppathy.R

Thiruppathy Ramachandran

unread,
Jan 1, 2014, 6:37:38 AM1/1/14
to rest...@googlegroups.com
I tested the REST services using SAOPUI and the services produce the results as expected. I need these services running fine with GWT as well.

Can you please help?

Thanks!

christian

unread,
Jan 1, 2014, 1:35:16 PM1/1/14
to rest...@googlegroups.com
JSONP can ONLY do GET

to use POST, PUT, etc where the js application comes from another server as the REST server is called CORS.

actually it is a server issue, since the server needs to all CORS requests. once that is done the your client with resty will just work ;)

on how to setup a server for CORS is beyond resty but there are some conversation in this mailing list - just search for CORS or find out how to setup your java server to serve CORS.

hope that helps a step forward.

-christian





--
You received this message because you are subscribed to the Google Groups "RestyGWT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to restygwt+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Thiruppathy Ramachandran

unread,
Jan 1, 2014, 5:22:47 PM1/1/14
to rest...@googlegroups.com
Thank you very much for details and You are right. The issue is due to the insufficient Header access in Server side. After the below code in the server, I'm able to successfully invoke from RESTYGWT.

response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "accept, x-http-method-override, content-type");


TCPMON helped me to find the headers being passed by RESTYGWT by default.


..Thiruppathy..R

On Wednesday, January 1, 2014 12:48:53 PM UTC+5:30, Thiruppathy Ramachandran wrote:

christian

unread,
Jan 2, 2014, 6:16:33 AM1/2/14
to rest...@googlegroups.com
you might want to add PUT in the Access-Control-Allow-Methods as well.

and for the headers I do it more general and copy the  'Access-Control-Request-Headers' request header over to the 'Access-Control-Allow-Headers' response header.

hope I find some time to add a little paragraph in the user-docs since this is a recurrent question ;)

- christian



--

Thiruppathy Ramachandran

unread,
Jan 2, 2014, 1:39:50 PM1/2/14
to rest...@googlegroups.com
Great! Appreciate that!
Reply all
Reply to author
Forward
0 new messages