I'm trying to invoke the Rest Services. The services is in the different server from where the GWT application is deployed.
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.
@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;
}
The above code is working fine with the JSONP callback. But not without JSONP callback.
public interface Service1 extends RestService {
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void testPost(Person request, MethodCallback<Response> callback);
Service1 service = GWT.create(Service1.class);
((RestServiceProxy)service).setResource(resource);
Person person = new Person();
service.testPost(person, new MethodCallback<Response>() {
public void onSuccess(Method method, Response response) {
public void onFailure(Method method, Throwable exception) {
The above code is failing with FailedStatusCodeException.