Restlet客户端如何处理媒体类型的请求? [英] How does Restlet client handle media type of request?
问题描述
我有几个关于Restlet客户端如何处理媒体类型标题的新手问题:
I've got several newbie questions about how Restlet client handles media type header:
- Restlet客户端将什么放入'内容 - 如果我传入类型为a的实体,则输入'标题'一个POJO或b)一个InputStream实例?那个'accept'标题会是什么?
- 如果我想在HTTP体中传输JSON格式的POJO,我是否需要序列化POJO并将其作为JSON字符串传递或者我可以通过POJO和Restlet将完成剩下的工作吗?如果是前一种情况,我是否需要指定'内容类型'标题以及如何?
非常感谢!
推荐答案
如果依赖ClientResource类,可以在put调用中添加MediaType参数,例如
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
实际上,额外的媒体类型参数定义了从远程资源接收的结果类型,但不适用于已发送的实体。
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.
对发送的实体/ POJO的媒体类型的控制基于ConverterService默认设置,取决于类路径上可用的扩展名及其各自的顺序。
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.
您可以通过直接调用
ClientResource cr = new ClientResource("http://targetDomain/path");
cr.put(cr.toRepresentation(myPojo, new Variant(MediaType.APPLICATION_JSON));
您还需要在类路径及其依赖项上添加org.restlet.ext.jackson扩展.XStream扩展是另一种选择。
You also need to add the org.restlet.ext.jackson extension on your classpath and its dependencies. The XStream extension is another option.
这篇关于Restlet客户端如何处理媒体类型的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!