在Jersey REST Client中设置内容类型/编码 [英] Setting content type/ encoding in Jersey REST Client

查看:124
本文介绍了在Jersey REST Client中设置内容类型/编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用jersey REST Client调用REST POST API.该API是docs is 网址: 方法:开机自检 标头信息:- X-GWS-APP-名称:XYZ 接受:application/json或application/xml

HI I have been trying to call REST POST API using jersey REST Client. The API is docs is URL: METHOD: POST Header Info:- X-GWS-APP-NAME: XYZ Accept: application/json or application/xml

我的Sample Jersey客户代码为

My Sample Jersey client code is

Client client = Client.create();

WebResource resource=client.resource(URL);

resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML);
resource.type(javax.ws.rs.core.MediaType.APPLICATION_XML);
resource.type("charset=utf-8");
ClientResponse response = resource.post(ClientResponse.class,myReqObj);

自上个星期以来,我一直在尝试这种代码变体,但它无法正常工作.在这方面的任何帮助都将受到高度赞赏.

I have been trying this code variation since last 1 week and it is not working. Any help in this regard is highly appreciated.

推荐答案

棘手的部分是WebResource方法遵循Builder设计模式,因此它返回一个Builder对象,您需要保留该对象并在调用其他方法时继续进行操作设置请求的完整上下文.

The tricky part is that the WebResource methods follows the Builder design pattern so it returns a Builder object which you need to preserve and carry on as you call further methods to set the full context of the request.

当您执行resource.accept时,它返回的内容不会存储,因此当您执行resource.type时它会丢失,因此只有您的上一次调用才会生效.

When you do resource.accept, it returns something you don't store, so it's lost when you do resource.type and therefore only your last call takes effect.

您通常会在一行中设置所有条件,但是您也可以将输出保存在本地变量中.

You'd typically set all the criterias in one line, but you could also save the output in a local variable.

ClientResponse response = client.resource(URL)
                                .accept(MediaType.APPLICATION_XML)
                                .type(MediaType.APPLICATION_XML)
                                .post(ClientResponse.class,myReqObj);

这篇关于在Jersey REST Client中设置内容类型/编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆