将其作为 Java 应用程序运行效果很好:
public class A {
public String getString(){
return "hey";
}
public String getString2(){
Gson gs = new Gson();
String a = gs.toJson("hello");
String b = gs.fromJson(a, String.class);
return b;
}
public static void main(String[] args) {
A a = new A();
System.out.println(a.getString());//returns "hey"
System.out.println(a.getString2());//returns "hello"
}
}
但是当我将它发布到 tomcat 并运行客户端时:
//client of A with AStub and ACallbackHandler autogenerated by eclipse
public class AClient {
public static void main(String[] args) {
AStub s = new AStub();
System.out.println(s.getString().get_return());
//returns "hey"
System.out.println(s.getString2().get_return());
//fails with org.apache.axis2.AxisFault: com/google/gson/Gson
}
}
此外,有时在重新发布 tomcat 服务器时它会显示 ClassNotFoundException
我正在使用 Eclipse EE Indigo、Axis2、GSON 1.7.1 和 tomcat 6
我做错了什么?
最佳答案
我没有添加外部 jar 文件! 将它们放在服务器的 WebContent/WEB-INF/lib 中。
关于java - 在网络服务中使用 gson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6957247/