How to call RESTFUL services from GWT?

前端 未结 6 1174
小蘑菇
小蘑菇 2020-12-08 10:59

I\'m using GWT as web development framework. I need to access some REST services from my GWT client code. Also I need to parse JSON (or maybe XML) which is response format o

6条回答
  •  春和景丽
    2020-12-08 11:13

    You can easily call a Restful web services using RestyGWT in your GWT application by creating proxy service interface:

    import javax.ws.rs.POST;
    ...
    public interface PizzaService extends RestService {
        @POST
        public void order(PizzaOrder request, 
                          MethodCallback callback);
    }
    

    or when you don't want to go through the trouble of creating service interfaces:

    Resource resource = new Resource( GWT.getModuleBaseURL() + "pizza-service");
    
    JSONValue request = ...
    
    resource.post().json(request).send(new JsonCallback() {
        public void onSuccess(Method method, JSONValue response) {
            System.out.println(response);
        }
        public void onFailure(Method method, Throwable exception) {
            Window.alert("Error: "+exception);
        }
    });
    

    It has also got nice API for encoding and decoding Java Object to JSON.

提交回复
热议问题