Thursday, September 5, 2013

Re: Call REST service from GWT App's client side

Access-Control-Allow-Origin is a response header, to be sent from the server. See http://www.w3.org/TR/cors/
Note that this rules out quite a few users though: http://caniuse.com/cors (note: GWT won't use XDomainRequest)
If you need to support IE8/IE9, then use deferred-binding and JSNI to use XDomainRequest in those browsers; or use another technique that works across browsers:

On Wednesday, September 4, 2013 11:50:12 PM UTC+2, SergeZ wrote:
Hi everyone! Please, help me figure out how can I manage to call the external REST service from GWT application's client code.

I've heard, that GWT has a RequstBuilder class in order to do such things, but suddenly I figured out that this is a quite ancient approach to implement. Hence, the following code won't work:

RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "http://localhost:19999/hello");
        builder.setHeader("content-type", "text/html;charset=windows-1251");
        builder.setHeader("Access-Control-Allow-Origin", "*");//result doesn not depend on this line of code

        try {
            Request response = builder.sendRequest("", new RequestCallback() {
                @Override
                public void onResponseReceived(Request request, Response response) {

                    answerFromRest.setText(request.toString() + "\n");
                    answerFromRest.setText(response.toString() + "\n");

                    int statusCode = response.getStatusCode();

                    if(statusCode == Response.SC_OK) {
                        String responseBody = response.getText();
                        answerFromRest.setText("The result is " + responseBody);
                    } else {
                        answerFromRest.setText("There was an error " + statusCode + " =" + response.getText());
                    }
                }

                @Override
                public void onError(Request request, Throwable exception) {
                    //error actually sending the request, never got sent
                }
            });
        } catch (RequestException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


statusCode always equals 0 though when I access my depolyed REST through a WebBrowser, I get the result. This makes me feel like there is really no more support for that code in GWT...

So, the question is - is there the other (working) way to call REST writing code in java ( I mean w/o using JSNI code to do that ).

Any way, whatever approach is ( if it exists in GWT's nowadays ), can you please provide either _code_ or _decent link with tutor or smth like that_ ?

Thank you very much, for the time spent on my subject!!!!



--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment