我在一个单独的域上有一个前端应用程序,它与另一个域上的Restlet后端进行通信。到目前为止,CORS正常工作,唯一的问题是bean序列化。
当我检查请求时,它有Accept
类型:application/x-java-serialized-object+gwt
但由于某种原因,我不知道后端何时在 localhost 上运行,我的GWT应用程序可以很好地向/从后端(在localhost)发送/接收数据,但是当后端部署在GAE云(即appspot.com)中,然后事情就会中断。它抛出422 Unprocessable Entity
上述问题的解决方案是什么?
我认为这是一个Restlet框架错误(我不确定)。现在我想要做的只是简单地下载序列化的GWT处理,只需在GWT ClientProxy端使用JSON或XML(application/json
或pplication/xml
),这可能吗?
这样我们应用的应用程序就可以了:
StuffResourceProxy stuffResource = GWT.create(StuffResourceProxy.class);
stuffResource.getClientResource().setReference("http://path.to/resource");
stuffResource.createStuff(model, new Result<Stuff>() {
@Override
public void onFailure(Throwable throwable) {
// handle error
}
@Override
public void onSuccess(Stuff stuff) {
// do thing with stuff
}
});
更新
这是我尝试过的内容,并添加:
stuffResource.getClientResource().getClientInfo().getAcceptedMediaTypes()
.add(new Preference<MediaType>(MediaType.APPLICATION_JSON));
GWT客户端能够发送数据并且服务器能够存储它,但是从Server-to-GWT-client端不起作用。
我可以从请求标题中看到GWT应用发送的Content-Type
类型为application/x-java-serialized-object+gwt
是否有办法强制Restlet ClientProxy发送application/json
而不是< / STRONG>?
但是现在服务器可以在服务器端处理application/x-java-serialized-object+gwt
POJO,问题在于客户端无法从服务器转换application/json
响应。
答案 0 :(得分:1)
没有明显的理由停止在客户端和服务器之间使用GWT序列化。我可以隔离一个示例代码,我很乐意调试它。
在向您展示检索json的方法之前,只需几句话。 GWT版本有一个名为org.restlet.ext.json的扩展(请参阅http://restlet.com/technical-resources/restlet-framework/guide/2.3/editions/gwt/json),它提供了一个JsonRepresentation类,并提供了一个简单处理JSON对象的方法。主要区别在于,通过这样做,您将只能访问GWT库提供的Json对象(例如JSONArray,JSONObject),而不能直接访问您自己的bean。这是一项非常困难的任务,需要在编译时动态地编写从有效负载到bean的反序列化代码。此任务已针对GWT序列化格式完成,因为作业的大部分已由GWT库提供。我可以向你保证,其他部分并不容易,请参阅https://github.com/restlet/restlet-framework-java/blob/master/modules/org.restlet/src/org/restlet/rebind/ClientProxyGenerator.java.gwt。
话虽如此,如果你想发送GWT序列化的有效载荷并接收json,你可以这样做:
e.g。
带注释的界面:
@Put
public void store(Contact contact, Result<String> callback);
更新module.gwt.xml
<inherits name="org.restlet.JSON" />
媒体类型偏好
stuffResource.getClientResource().accept(MediaType.APPLICATION_JSON);
响应消耗(详细信息):
public void onSuccess(String response) {
try {
JsonRepresentation jRep = new JsonRepresentation(response);
dialogBox.setText("Update contact"
+ jRep.getJsonObject().get("lastName"));
} catch (IOException e) {
}
我希望这会对你有所帮助。