使用JSONP连接REST和GWT时出错

时间:2013-01-09 12:29:45

标签: rest gwt jsonp

我创建了一个REST Web服务,然后我尝试使用JSNOP通过GWT访问它。 我没有得到结果字符串。

我在Chrome中得到了回应。以下我通过点击Inspect Element->获得了chrome。网络 - > GET->接头

Request URL:http://localhost:8080/RestWeb/webresources/generic/get?callback=__gwt_jsonp__.P0.onSuccess
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:JSESSIONID=f2976e5c04000cb9b46eb992128f
Host:localhost:8080
Referer:http://127.0.0.1:8888/Mygwtclientapplication.html?gwt.codesvr=127.0.0.1:9997
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
Query String Parametersview URL encoded
callback:__gwt_jsonp__.P0.onSuccess
Response Headersview source
Content-Type:application/json
Date:Wed, 09 Jan 2013 12:00:40 GMT
Server:GlassFish Server Open Source Edition 3.1.2.2
Transfer-Encoding:chunked
X-Powered-By:Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Sun Microsystems Inc./1.6)

点击Inspect Element-> Console后出现以下错误

未捕获的SyntaxError:意外的标识符get:1

点击Inspect Element->网络 - > get->响应我得到了以下数据

content from get method

下面给出的REST服务中的资源类。

@Path("generic")
@WebService
public class GenericResource {

@Context
private UriInfo context;
@Context
private HttpServletResponse response;
private String content = "content";

/**
 * Creates a new instance of GenericResource
 */
public GenericResource() {
}

/**
 * Retrieves representation of an instance of
 * com.mycompany.resource.GenericResource
 *
 * @return an instance of java.lang.String
 */
@GET

@Path("/get")
@Produces("application/json,text/plain")
public String getXml() {
    System.out.println("GET");
    //response.addHeader(content, content);

    return this.content + " from get method";

}

以下是我的GWT入口点类

public class Mygwtclientapplication implements EntryPoint {



    public void onModuleLoad() {

        JsonpRequestBuilder jsonpRequestBuilder=new JsonpRequestBuilder();
        System.out.println(jsonpRequestBuilder.getCallbackParam());
        jsonpRequestBuilder.requestString("http://localhost:8080/RestWeb/webresources/generic/get", new AsyncCallback<String>() {

            @Override
            public void onFailure(Throwable caught) {

                System.out.println(caught.getMessage());

            }

            @Override
            public void onSuccess(String result) {

                System.out.println(result);


            }
        });

    }
}

执行后,我在eclipse控制台中得到以下内容

Timeout while calling http://localhost:8080/RestWeb/webresources/generic/get

0 个答案:

没有答案