开发者

GWT and REST (jax-rs)

开发者 https://www.devze.com 2023-01-30 13:00 出处:网络
I have a project where you can ask for resources that are served by jax-rs in the json format. Everything works properly in the browser when I query the rest URL the json appears.

I have a project where you can ask for resources that are served by jax-rs in the json format. Everything works properly in the browser when I query the rest URL the json appears.

Now I want my GWT project to request those resources and process them and show them in my interface. The simplest way I found to do so is using a request builder and an overlay. Code is lower. The problem is, it seems when the code is running it never goes into the actual RequestCallback(). The status string is never changed. I thought it could be a SOP so I added the <add-linker name="xs"/> but still doesn't work. Any ideal?

    package com.workoutcell.client;
    //import com.google.gwt.core.client.JavaScriptObject;
    import com.google.gwt.core.client.JsArray;
    import com.google.gwt.http.client.*;
    import com.google.gwt.http.client.Request;
    import com.google.gwt.http.client.RequestBuilder;
    import com.google.gwt.http.client.RequestCallback;
    import com.google.gwt.http.client.RequestException;
    import com.google.gwt.http.client.Response;

    /**
     *
     * @author 
     */

    public class RestToInfoSession{

        String queryReturn = null;
        JsArray<InfoJSO> arrayOfInfo = null;
        String host = "http://localhost:8080/mysite";
        String restModule = "/calendar/getinfo";
        String id = null;
        String year = null;
        String month = null;
        String status = "Not Initialized";

        public RestToInfoSession(String id, String year, String month){

            this.id =id;
            this.year = year;
            this.month = month;
            String url = host + restModule + "/"+this.id + "/"+this.year + "/"+this.month;   
            RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);


            try {
                status = "Initialized at Url " + builder.getUrl();
                Request request = builder.sendRequest(null, new RequestCallback() {

                    public void onError(Request request, Throwable exception) {
                        // Couldn't connect to server (could be timeout, SOP violation, etc.)
                         status = "Error on connecting to Server";
                    }


                    public void onResponseReceived(Request request, Response response) {
                        if (200 == response.getStatusCode()) {
                           // arrayOfInfo = jsonToJsArray(response.getText());    
                            status = "JSON has been Fetched. Result is:" + response.getText();
                        } else if(0 == response.getStatusCode()) {
                            status = "Error is 0";
                        } else {
                            status = "Error in JSON Request:" + response.getStatusCode();
                            //response.getStatusText();
                        }
                    }

                });

            } catch (RequestException ex) {
                 status = "Error in  Request Builder Startup";
            }

        }

        //get an jso object in array
        private final native JsArray<InfoJSO> jsonToJsArray(String json) /*-{
        return eval(json);
          }-*/;

        public JsArray<InfoJSO> getInfoArray (){

            return arrayOfInfo;
        }

    }

UPDATE: My problem is the same as Referring to a non-final variab开发者_开发百科le data inside an inner class . I wasn't aware of asynchronous calls working mechanism. I still don't know how to pass my response.getText() to update a label that isn't part of my RestToInfoSession class any ideas?


Consider using the RestyGWT project. It will make calling JAXRS JSON resources as easy as using GWT-RPC. Plus you can typically reuse the same request response DTOs from the server side on the client side.


I have put a timer that checks every 1000ms if my json string has updated from null to the xhttp requested data. This works, but I got a feeling there is a more elegant way of resolving this problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号