问题描述
我在另一个域上有一个前端应用程序,它与另一个域上的Restlet后端进行通信。到目前为止,CORS正常工作,唯一的问题是bean序列化。
当我检查请求时,它有 Accept 类型: application / x-java -serialized-object + gwt
然而,由于某些原因,我不知道后端在 localhost 我的GWT应用程序在向后端(在本地主机)发送/接收数据时工作正常,但是当后端部署在GAE云(即appspot.com)中时,事情就会中断。它会抛出 422无法处理的实体
上述问题的解决方案可能是什么?
我认为这是一个Restlet框架错误(我不确定)。现在我想要做的只是简单地取消序列化的GWT处理,并使用JSON或XML( application / json 或应用程序/ xml )在GWT ClientProxy方面,这是可能的吗?
这样我们有同样的应用程序应该工作:
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){
//处理错误
}
@Override
public void onSuccess(Stuff stuff){
// do something with stuff
}
});
更新:
$ b
stuffResource.getClientResource()。getClientInfo()。getAcceptedMediaTypes()
.add(new Preference< MediaType>(MediaType.APPLICATION_JSON));
GWT客户端能够发送数据,而服务器能够存储它, GWT客户端不起作用。
我可以从请求头中看到,GWT应用程序发送的 Content-Type 类型是 application / x-java-serialized-object + gwt 有没有办法强制Restlet ClientProxy发送 application / json 而不是?
现在,服务器可以处理 application / x-java-serialized-object + gwt POJO服务器端,问题是客户端无法从服务器转换 application / json 响应。
在向您展示检索json的方法之前,请稍等片刻。
GWT版本的扩展名为org.restlet.ext.json(请参阅),它提供了一个JsonRepresentation类,并提供了一个平均值简单处理JSON对象。主要区别在于,通过这样做,您将只能访问由GWT库提供的Json对象(如JSONArray,JSONObject),而不是直接访问您自己的Bean。这是一项非常困难的任务,需要在编译时动态编写从有效负载到bean的反序列化代码。这个任务已经完成了GWT序列化格式,因为大部分工作都已经由GWT库提供。我可以向你保证,其他部分不是很容易,请参阅。
话虽如此,如果你想发送GWT序列化的有效载荷,并接收json,你可以做如下:
- 更新已注释的接口,以便将字符串接收为有效内容。
- 导入org.restlet。 client.ext.json扩展到您的项目
- 为任何客户端资源实例指定媒体类型首选项
- 手动实例化具有字符串值的JsonRepresentation
- 导入org.restlet。 client.ext.json扩展到您的项目
例如
带注释的界面:
@Put
public void store(Contact contact,Result< String> callback);
更新module.gwt.xml
< inherits name =org.restlet.JSON/>
媒体类型首选项
stuffResource.getClientResource()接受(MediaType.APPLICATION_JSON);
响应消耗(详细信息):
public void onSuccess(String response){
try {
JsonRepresentation jRep = new JsonRepresentation(response);
dialogBox.setText(更新联系人
+ jRep.getJsonObject()。get(lastName));
} catch(IOException e){
}
我希望这会有所帮助你。
I have a Front-end application on a separate domain that is communicating with the Restlet backend on a different domain. So far, CORS work properly and the only issue is the bean serialization.
As I check the request it has this Accept type: application/x-java-serialized-object+gwt
However for some reason I don't know when the backend runs on localhost my GWT app works fine in sending/receiving data to/from the backend (at localhost), but when the backend is deployed in GAE cloud (i.e. appspot.com) then things break. It throws 422 Unprocessable Entity
What could be a solution for the problem above?
I think this is a Restlet framework bug (I am not sure). Now what I want to do now is to simply just get off the serialized GWT processing and just use either JSON or XML (application/json or application/xml) on the GWT ClientProxy side, is that possible?
Such that the same app we have should work:
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 } });
Update:
Here's what I have tried, adding:
stuffResource.getClientResource().getClientInfo().getAcceptedMediaTypes() .add(new Preference<MediaType>(MediaType.APPLICATION_JSON));
GWT client was able to send data and server was able to store it, however from Server-to-GWT-client side does not work.
I can see from the request headers that the Content-Type type sent by the GWT app is application/x-java-serialized-object+gwt is there a way to force Restlet ClientProxy to send out application/json instead?
Yet right now the server can process that application/x-java-serialized-object+gwt POJO on the server side, the issue is with the client side not being able to convert the application/json response from server.
there is no evident reason to stop using GWT serialization between client and server. I you can isolate a sample code, I will be pleased to debug it.
Just a few words before showing you a way to retrieve json.There is an extension for the GWT edition called org.restlet.ext.json (see http://restlet.com/technical-resources/restlet-framework/guide/2.3/editions/gwt/json) which provides a JsonRepresentation class, and provides also a mean to simple handle JSON objects. The main difference is that by doing so, you will only have access to the Json objects provided by GWT library (such as JSONArray, JSONObject), not to your own bean directly. This is a quite difficult task, that require to write dynamically, at compilation time, the deserialization code from the payload to the beans. This task has been done for the GWT serialization format, because most part of the job were already provided by the GWT library. I can assure you the other part is not very easy, see https://github.com/restlet/restlet-framework-java/blob/master/modules/org.restlet/src/org/restlet/rebind/ClientProxyGenerator.java.gwt.
Having said that, if you want to send GWT-serialized payload, and receive json, you can do as follow:
- update the annotated interface in order to receive the payload as a String.
- import the org.restlet.client.ext.json extension to your project
- specify the media type preference to any instance of client resource
- Instantiate manually a JsonRepresentation with the String value
e.g.
Annotated interface:
@Put public void store(Contact contact, Result<String> callback);
Update of the module.gwt.xml
<inherits name="org.restlet.JSON" />
Media type preference
stuffResource.getClientResource().accept(MediaType.APPLICATION_JSON);
Response consumption (detail):
public void onSuccess(String response) { try { JsonRepresentation jRep = new JsonRepresentation(response); dialogBox.setText("Update contact" + jRep.getJsonObject().get("lastName")); } catch (IOException e) { }
I hope this will help you.
这篇关于Restlet ClientProxy和GAE服务器上的422不可处理实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!