如何使用Jersey Client API在RESTful呼叫上添加标题 [英] How to add Headers on RESTful call using Jersey Client API

查看:140
本文介绍了如何使用Jersey Client API在RESTful呼叫上添加标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 头文件:
Content-Type:application / json; charset = UTF-8
Authorization:Bearer Rc7JE8P7XUgSCPogjhdsVLMfITqQQrjg

REQUEST:
GET https://api.example.com/1/realTime?json= {selection:{ includeAlerts:true,selectionType:registered,selectionMatch:,isTheEvent:true,includeRuntime:true}}


以下是我的代码:

  try 
{
客户端客户端= Client.create();
WebResource webResource =
client.resource(https://api.example.com/1/realTime?json=
{selection:{includeAlerts:true, selectionType: 注册, selectionMatch: , isTheEvent: 真, includeRuntime: 真}});
$ b $ //添加头文件:Content-Type:application / json; charset = UTF-8
webResource.setProperty(Content-Type,application / json; charset = UTF-8 );

//添加标题:授权承载Rc7JE8P7XUgSCPogsdfdLMfITqQQrjg
value =承载+值;
webResource.setProperty(Authorization,value);

多值图< String,String> queryParams = new MultivaluedMapImpl();
queryParams.add(json,js);

//从RESTful Server获取响应
jsonStr = webResource.get(String.class);
System.out.println(Testing:);
System.out.println(jsonStr);

$ b catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
System.exit(0);
}

但它返回错误

  com.sun.jersey.api.client.UniformInterfaceException:GET https://api.example.com/1/realTime?通过com.sun.jersey.api.client.WebResource.handle(WebResource.java:607)返回响应状态500 
com.sun.jersey.api.client.WebResource.get($) WebResource.java:187)
at net.yorkland.restful.GetThermostatlist.GetThermostats(GetThermostatlist.java:60)

我想我没有正确添加标题。

有人可以帮我解决吗?请给我建议如何根据请求添加标题。



谢谢

解决方案

我认为你正在寻找标题(名称,值)方法。请参阅 WebResource.header(String,Object)

注意它会返回一个Builder,所以你需要将输出保存在你的webResource变量中。


Here is the Format for RESTful call:

HEADERS:
    Content-Type: application/json;charset=UTF-8
    Authorization: Bearer Rc7JE8P7XUgSCPogjhdsVLMfITqQQrjg

REQUEST:
    GET https://api.example.com/1/realTime?json={"selection":{"includeAlerts":"true","selectionType":"registered","selectionMatch":"","isTheEvent":"true","includeRuntime":"true"}}

Here is my codes:

                try
                {
                 Client client = Client.create();
                 WebResource webResource = 
                         client.resource("https://api.example.com/1/realTime?json=
                         {"selection":{"includeAlerts":"true","selectionType":"registered","selectionMatch":"","isTheEvent":"true","includeRuntime":"true"}}");

                 //add header:Content-Type: application/json;charset=UTF-8
                 webResource.setProperty("Content-Type", "application/json;charset=UTF-8");

                 //add header: Authorization Bearer Rc7JE8P7XUgSCPogsdfdLMfITqQQrjg
                 value = "Bearer " + value;
                 webResource.setProperty("Authorization", value);

                 MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
                 queryParams.add("json", js);

                 //Get response from RESTful Server
                 jsonStr = webResource.get(String.class);
                 System.out.println("Testing:");
                 System.out.println(jsonStr);

                }
                catch (Exception e)
                {
                  System.out.println (e.getMessage());
                  e.printStackTrace();
                  System.exit(0);
                }

But it returns error

com.sun.jersey.api.client.UniformInterfaceException: GET https://api.example.com/1/realTime? returned a response status of 500
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:607)
    at com.sun.jersey.api.client.WebResource.get(WebResource.java:187)
    at net.yorkland.restful.GetThermostatlist.GetThermostats(GetThermostatlist.java:60)

I think I didn't add headers correctly.

Can someone help me to fix it? Please give me advice how to add headers on request.

Thank you

解决方案

I think you're looking for header(name,value) method. See WebResource.header(String, Object)

Note it returns a Builder though, so you need to save the output in your webResource var.

这篇关于如何使用Jersey Client API在RESTful呼叫上添加标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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