问题
I've got several newbie questions about how Restlet client handles media type header:
- What will Restlet client put in 'content-type' header if I pass in an entity of type a) a POJO or b) an InputStream instance? And what the 'accept' header will be?
- If I want to transfer a POJO in JSON format in HTTP body, do I need to serialize the POJO and pass it in as a JSON string or can I just pass in the POJO and Restlet will do the rest? If it's the former case, do I need to specify the 'content-type' header and how?
Thanks a lot!
回答1:
If you rely on the ClientResource class, you can add a MediaType parameter to your put call, such as
put(myPojo, MediaType.APPLICATION_JSON);
UPDATE Actually, the extra media type parameter defines the accepted result type expected from the remote resource, but doesn't apply to the sent entity.
The control on the media type of the entity/POJO sent is based on the ConverterService default settings, which depends on the extensions available on your classpath and their respective order.
You can have full control by directly invoking the
ClientResource cr = new ClientResource("http://targetDomain/path");
cr.put(cr.toRepresentation(myPojo, new Variant(MediaType.APPLICATION_JSON));
You also need to add the org.restlet.ext.jackson extension on your classpath and its dependencies. The XStream extension is another option.
来源:https://stackoverflow.com/questions/23630094/how-does-restlet-client-handle-media-type-of-request