如何在gwt web应用程序的后端运行实际的java

时间:2012-03-09 13:52:08

标签: java gwt servlets request response

对于我的项目中的特定要求,我想从Web服务api中检索JSON响应。

我在一个运行正常的简单Java项目中尝试了一个java代码。

String message = null;
HttpClient httpclient = new DefaultHttpClient(); 
JSONParser parser = new JSONParser();

String url = "working - url";
HttpResponse response = null;
response = httpclient.execute(new HttpGet(url));

JSONObject json_data = null;
json_data = (JSONObject)parser.parse(EntityUtils.toString(response.getEntity()));
JSONArray results = (JSONArray)json_data.get("result");
for (Object queid : results) {
    message = message.concat((String) ((JSONObject)queid).get("id"));
    message = message.concat("\t");
    message = message.concat((String) ((JSONObject)queid).get("owner"));
    message = message.concat("\n");
}

如果我尝试在GWT应用程序servlet中运行此代码,我会收到编译错误。

[ERROR] Line 16: No source code is available for type org.apache.http.client.ClientProtocolException; did you forget to inherit a required module?
[ERROR] Line 16: No source code is available for type org.apache.http.ParseException; did you forget to inherit a required module?
[ERROR] Line 16: No source code is available for type org.json.simple.parser.ParseException; did you forget to inherit a required module?

我已使用项目构建路径添加了所需的jar - >添加外部jar。 但我仍面临同样的问题。 我已经尝试将源附加到罐子中,但问题仍然是相同的。

在这种情况下可能做些什么? 我可以在GWT应用程序的后端运行实际的java

编辑#1:

此代码是在我的GWT servlet中编写的。我已经尝试使用throws子句添加到我的servlet中的方法并应用try / catch块。但我仍然遇到这些错误。

编辑#2:

应用try / catch块后,我的控制台日志中出现以下错误。

Mar 10, 2012 1:16:28 PM com.google.appengine.tools.development.ApiProxyLocalImpl log
SEVERE: javax.servlet.ServletContext log: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.lang.String com.google.detracker.client.DeService.getJSONRespnse()' threw an unexpected exception: java.lang.NoClassDefFoundError: javax.net.ssl.KeyManagerFactory is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:58)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:70)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:351)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.NoClassDefFoundError: javax.net.ssl.KeyManagerFactory is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51)
    at org.apache.http.conn.ssl.SSLSocketFactory.createSSLContext(SSLSocketFactory.java:184)
    at org.apache.http.conn.ssl.SSLSocketFactory.createDefaultSSLContext(SSLSocketFactory.java:209)
    at org.apache.http.conn.ssl.SSLSocketFactory.<init>(SSLSocketFactory.java:333)
    at org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(SSLSocketFactory.java:165)
    at org.apache.http.impl.conn.SchemeRegistryFactory.createDefault(SchemeRegistryFactory.java:45)
    at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:294)
    at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:445)
    at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:274)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:797)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
    at com.google.detracker.server.DeServiceImpl.getJSONRespnse(DeServiceImpl.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:100)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
    ... 34 more

Mar 10, 2012 1:16:28 PM com.google.appengine.tools.development.LocalResourceFileServlet doGet

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

如果你注意,错误是关于异常类。这意味着您的代码返回了您未处理的异常。 type org.apache.http.client.ClientProtocolException

尝试用try catch块代替你的代码,看看你在开发控制台中得到了什么。

Try {
    String message = null;
    HttpClient httpclient = new DefaultHttpClient(); 
    JSONParser parser = new JSONParser();

    String url = "working - url";
    HttpResponse response = null;
    response = httpclient.execute(new HttpGet(url));

    JSONObject json_data = null;
    json_data = (JSONObject)parser.parse(EntityUtils.toString(response.getEntity()));
    JSONArray results = (JSONArray)json_data.get("result");
    for (Object queid : results) {
        message = message.concat((String) ((JSONObject)queid).get("id"));
        message = message.concat("\t");
        message = message.concat((String) ((JSONObject)queid).get("owner"));
        message = message.concat("\n");
    }
} catch (Exception e) {
    e.printStack();
}

说明:

为所有想要它的人提供更多解释。在您的代码中,您不会使用try catch blocks处理异常。因此,当引发异常时,服务器会将其发送回客户端,但大部分异常都是 NOT serializable,因此无法将其转回对客户而言,这就是你得到上述错误的原因。

答案 2 :(得分:0)

是的,你可以,但错误信息表明你在客户端代码的某个地方导入了一个使用这些服务器端外部jar的类。

编辑:根据Adel Boutros的答案,我猜你已经为生成这些错误的类添加了throws到你的GWT服务接口?您只能抛出客户端也可用的异常,因此请在服务器上捕获它们并重新抛出客户端中可用的新异常。

答案 3 :(得分:-1)

如果这些librabries实际上可以在GWT中使用,则必须在application.gwt.xml文件中添加行来继承模块。

库smartgwt的示例:

<?xml version="1.0" encoding="UTF-8"?>

<!-- Inherit the core Web Toolkit stuff.                        -->
<inherits name='com.google.gwt.user.User'/>

<!-- Other module inherits                                      -->
<inherits name="com.smartgwt.SmartGwt"/>

<!-- Specify the app entry point class.                         -->
<entry-point class='blablabla'/>

<!-- Specify the paths for translatable code                    -->
<source path='client'/>
<source path='shared'/>