How does Restlet client handle media type of request?

痴心易碎 提交于 2020-01-30 08:11:46

问题


I've got several newbie questions about how Restlet client handles media type header:

  1. 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?
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!