我创建了一个 REST Web 服务,然后尝试使用 JSNOP 通过 GWT 访问它。我没有得到结果字符串。
我得到了铬的回应。以下是我通过单击检查元素-> 网络-> 获取-> 标头从 chrome 获得的
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:意外的标识符获取:1
通过单击 Inspect Element-> Network->get->Response 我得到了以下数据
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