自定义HTTPHeader RestTemplate

时间:2018-01-11 14:06:51

标签: spring-boot http-headers resttemplate

在哪里检查是否添加了以下标题?我检查了Firefox,它没有出现。以下代码取自http://springinpractice.com/2013/10/27/how-to-send-an-http-header-with-every-request-with-spring-resttemplate

private HttpHeaders createHttpHeaders() {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("X-User-Agent", "test");
    return headers;
}

@RequestMapping("/testClient")
public void test() {

     logger.info("calling ws");
     HttpHeaders headers = createHttpHeaders();
     HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
     ResponseEntity<Boolean> response = restTemplate.exchange("http://localhost:8081/test", HttpMethod.GET, entity, Boolean.class);
     if (response.getStatusCode().equals(HttpStatus.OK)) {
         logger.info("getbody -" + response.getBody());
     }
}

0 个答案:

没有答案