在Web服务中使用gson

时间:2011-08-05 13:41:27

标签: java web-services tomcat soap gson

这可以很好地运行它作为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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我没有添加外部jar文件! 将它们放在服务器的WebContent / WEB-INF / lib中。