使用Restlet ClientResource无法将实体发送到Restlet服务 [英] Using Restlet ClientResource cannot send entity to Restlet Service
问题描述
我在使用Restlet客户端调用正常的休息服务时遇到问题:
I'm having problem with Restlet client call to a working rest service:
final ClientResource resource = new ClientResource(Routes.PUBLIC_STORE_API);
resource.setOnResponse(new Uniform() {
public void handle(Request request, Response response) {
try {
Status status = response.getStatus();
if(!Status.isError(status.getCode())){
String jsonResponse = response.getEntity().getText();
} else {
// Handle error
}
} catch (Exception e){
callback.failure(new Throwable(e.getMessage()));
}
}
});
JsniHelper.consoleLog("Adding object id=" + id + " type=" + type + " data=" + jsonObject);
resource.getReference().addQueryParameter("type", type);
resource.getReference().addQueryParameter("id",id);
resource.post(jsonObject, MediaType.APPLICATION_JSON);
在对象上方的日志位置,是一个有效的JSON字符串.
At the point of log above the object is a valid JSON string.
Restlet ServerResource
能够同时获取id
和type
字符串,但是实体始终为null
:
The Restlet ServerResource
is able to get both the id
and type
Strings however the entity is always null
:
@Post("json")
public Representation add(Representation entity){ // omitted }
我尝试使用CURL,并且Rest服务能够正确处理JSON字符串.
I tried to use CURL and the Rest service was able to process the JSON string properly.
我在GWT中的代码可能是什么问题?
What could be the problem with my code in GWT?
更新:我在POM中有此
<dependency>
<groupId>org.restlet.gae</groupId>
<artifactId>org.restlet.ext.gwt</artifactId>
<version>2.2-RC3</version>
</dependency>
推荐答案
为使此调用运行,请执行以下操作:
In order to make this call run do the following things:
- 将GWT版本中使用的框架的Json扩展(org.restlet.ext.json.jar)添加到您的项目中
-
将以下依赖项添加到您的GWT模块中
- add the Json extension (org.restlet.ext.json.jar) of the framework taken from the GWT edition to your project
add the following dependency to your GWT module
然后按照以下步骤更新您的代码:
then update your code as follow:
resource.post(新的JsonRepresentation(MediaType.APPLICATION_JSON,jsonObject));
resource.post(new JsonRepresentation(MediaType.APPLICATION_JSON, jsonObject));
我注意到,如果您发送的是表示形式而不是对象,则它会起作用. 主要的问题是,在处理对象时,核心模块不会如何对其进行序列化.在JVM中,我们可以插入一个服务,该服务发现可以将bean自动转换为表示形式的可用转换器,而我们无法使用GWT来实现.
I notice that it works if you send a representation, instead of an object. The main problem is that when handling an object, the core module does not how to serialize it. In a JVM, we can plug a service that discovers the available converters that automagically convert a bean into a representation, we can't achieve this using GWT.
话虽如此,我认为您可以通过使用Bean来更轻松地集成客户机和服务器代码. GWT客户端代码可以使用特定的GWT序列化格式进行通信,其他任何客户端代码也可以使用JSON进行通信.
Having said that, I think you can integrate your client and server code in a easier way, by just using beans. The GWT client code can communicate using the specific GWT serialization format, any other client code using JSON.
这篇关于使用Restlet ClientResource无法将实体发送到Restlet服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!