开发者

Java + GWT + GSON on server side

开发者 https://www.devze.com 2023-01-02 16:46 出处:网络
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.

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 invokes testService.testMethod();
  • TestService interface and开发者_StackOverflow社区 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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消