Java + GWT +服务器端的GSON
我已经读到不可能在 GWT 客户端代码中运行 GSON,但是可以在服务器代码中运行它。
后一个是我正在努力实现的目标,但还没有开始工作。我认为 com.whatever.server 包中的任何类都可以访问整个 JRE 命名空间,包括反射。看来事实并非如此。那么如何管理所有这些开发人员在 GWT 服务器代码中使用 GSON? (我是 GWT 新手,所以答案可能非常简单。)
谢谢。
编辑:我的设置如下:
- test.gwt.xml包含:
和
- 客户端使用
private final TestServiceAsync testService = GWT.create(TestService.class);
并调用testService.testMethod();
- TestService 接口和 TestServiceAsync位于包客户端中,
- 在 test.server 中实现的 TestServiceImpl 使用
Gson g = new Gson();
等等
抛出的异常是 java.lang.NoClassDefFoundError: com/google/gson/ Gson。但似乎无论该方法是否包含 GSON 代码(只要包含 GSON import-statement 就足够了)都会引发该问题。
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:151) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) ... 33 more
I already read that there is no possibility to run GSON in GWT client code, but that it is possible to run it in server code.
The latter one is which I'm trying to achive, but not getting to work. I thought any class within the com.whatever.server package has access to the whole JRE namespace including reflection. It seems that that is not the case. So how managed all those developers to use GSON in GWT server code? (I'm new to GWT, so the answer may be really easy.)
Thanks.
EDIT: My setting is as follows:
- test.gwt.xml contains:
<source path='client'>
and<source path='shared'>
- the client uses
private final TestServiceAsync testService = GWT.create(TestService.class);
and invokestestService.testMethod();
- TestService interface and TestServiceAsync are located in package client
- the TestServiceImpl implemented in test.server uses
Gson g = new Gson();
and so forth
The exception thrown is java.lang.NoClassDefFoundError: com/google/gson/Gson. But it seems that it is raised no matter whether the method contains GSON code or not (it is sufficient that it contains the GSON import-statement).
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:151) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) ... 33 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个类路径问题。 GSON 不在类路径中。您使用哪个 IDE?如果你使用 eclipse -
1.右键单击项目
2.构建路径
3.配置构建路径
4.选择“库”选项卡
5.单击“添加罐子...”
6. 在弹出窗口中为每个必要的罐子选择正确的位置。
This is a class path problem. GSON is not in the classpath. Which IDE do you use? if u use eclipse -
1. right click the project
2. build path
3. configure build path
4. select "libraries" tab
5. click "add jars..."
6. in the popup choose the correct location for each of the necessary jars.
对于 JSP/Servlet Web 应用程序,您只需将第 3 方 JAR 文件放入 /WEB-INF/lib 文件夹中。如果项目是动态 Web 项目,那么 Eclipse 也会自动注意正确设置构建路径。您不需要摆弄 Eclipse 构建路径。不要忘记撤消这一切
In case of a JSP/Servlet webapplication, you just need to drop 3rd party JAR files in /WEB-INF/lib folder. If the project is a Dynamic Web Project, then Eclipse will automatically take care about setting the buildpath right as well. You do not need to fiddle with Eclipse buildpath. Don't forget to undo it all