我将 GWT 用于客户端应用程序和 REST Web 服务而不是服务器(不在 GWT 中使用 RPC 或 servlet)。我想将 java 对象转换为 JSON 并将 JSON 对象从客户端传递到服务器以及服务器到客户端。但是对于处理我想将 JSON 对象转换为 java 对象 i 服务器。GWT 不支持 GSON 有没有办法将 java 对象转换为 JSON 对象,反之亦然?
我使用 Autobean 框架将 java 对象转换为 JSON 做了一个示例项目,但出现以下错误
[ERROR] [gwtmodules] - Deferred binding result type 'com.mycompany.gwtmodules.client.MyFactory' should not be abstract
] Failed to create an instance of 'com.mycompany.gwtmodules.client.Gwtmodules' via deferred binding
java.lang.RuntimeException: Deferred binding failed for 'com.mycompany.gwtmodules.client.MyFactory' (did you forget to inherit a required module?)
以下是我的代码
public interface Test {
public int getAge();
public void setAge(int age);
public String getName();
public void setName(String name);
}
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;
public interface MyFactory extends AutoBeanFactory{
AutoBean<Test> test();
}
public class Gwtmodules implements EntryPoint {
MyFactory factory = GWT.create(MyFactory.class);
/**
* This is the entry point method.
*/
Test makeTest() {
// Construct the AutoBean
AutoBean<Test> test = factory.test();
// Return the Person interface shim
return test.as();
}
Test deserializeFromJson(String json) {
AutoBean<Test> bean = AutoBeanCodex.decode(factory, Test.class, json);
return bean.as();
}
String serializeToJson(Test test) {
// Retrieve the AutoBean controller
AutoBean<Test> bean = AutoBeanUtils.getAutoBean(test);
return AutoBeanCodex.encode(bean).getPayload();
}
public void onModuleLoad() {
final Test test = makeTest();
test.setAge(44);
test.setName("achu");
final Button button = new Button("Click here");
button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String s = serializeToJson(test);
Window.alert("alert" + s);
}
});
}
}