问题
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);
At the point of log above the object is a valid JSON string.
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 }
I tried to use CURL and the Rest service was able to process the JSON string properly.
What could be the problem with my code in GWT?
Update: I have this in the POM
<dependency>
<groupId>org.restlet.gae</groupId>
<artifactId>org.restlet.ext.gwt</artifactId>
<version>2.2-RC3</version>
</dependency>
回答1:
In order to make this call run do the following things:
- 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(new JsonRepresentation(MediaType.APPLICATION_JSON, jsonObject));
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.
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.
Have a look at this code: http://restlet.com/technical-resources/restlet-framework/guide/2.3/introduction/first-steps/first-application
来源:https://stackoverflow.com/questions/27738573/using-restlet-clientresource-cannot-send-entity-to-restlet-service