简体   繁体   English

Restlet客户端如何处理请求的媒体类型?

[英]How does Restlet client handle media type of request?

I've got several newbie questions about how Restlet client handles media type header: 我有几个关于Restlet客户端如何处理媒体类型标题的新手问题:

  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? 如果我传入类型a)POJO或b)InputStream实例的实体,Restlet客户端将在“ content-type”标头中放入什么? And what the 'accept' header will be? 那么“ accept”头将是什么?
  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? 如果我想在HTTP主体中以JSON格式传输POJO,我是否需要序列化POJO并将其作为JSON字符串传递,还是可以仅传递POJO而Restlet会完成其余工作? If it's the former case, do I need to specify the 'content-type' header and how? 如果是前一种情况,我是否需要指定“ content-type”标头以及如何指定?

Thanks a lot! 非常感谢!

If you rely on the ClientResource class, you can add a MediaType parameter to your put call, such as 如果您依赖ClientResource类,则可以将MediaType参数添加到put调用中,例如

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. 对发送的实体/ POJO的媒体类型的控制基于ConverterService的默认设置,该默认设置取决于类路径上可用的扩展及其各自的顺序。

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. 您还需要在类路径及其依赖项上添加org.restlet.ext.jackson扩展名。 The XStream extension is another option. XStream扩展是另一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM