2016-12-02 26 views
0

我得到了一个web服务的url,它以json格式返回值,但它需要获取请求中的标题信息作为关键值对,例如,我需要传递Emp_code作为关键字和'xyz'作为值,以获取邮递员中所有员工的详细信息。如何通过标题信息作为键值对使用弹簧消费休息服务

private static void getEmployees() 

{ 最后字符串的uri = “http://abc/springrestexample/employees”;

RestTemplate restTemplate = new RestTemplate(); 
String result = restTemplate.getForObject(uri, String.class); 

System.out.println(result); 

}

在上面的代码中如何可以通过报头信息(键值),以消耗的服务。

回答

1

您可以通过下面的例子中添加页眉您的要求:

org.springframework.http.HttpHeaders requestHeaders = new HttpHeaders(); 
requestHeaders.add("yourHeaderKey", "yourHeadeerValue"); 
org.springframework.http.HttpEntity<?> httpEntity = new HttpEntity<>(requestHeaders); 

然后发出此调用您restTemplate:

restTemplate.exchange(uri, org.springframework.http.HttpMethod.GET, httpEntity , String.class);