RestTemplateBuilder
あるRestTemplate
で固定のheaderがあるならRestTemplateBuilder
を使用する。
@Component
public class RestTemplateSample {
final RestTemplate client;
public RestTemplateSample (RestTemplateBuilder builder) {
client = builder
.additionalInterceptors((request, body, execution) -> {
request.getHeaders().set("my-header", "hogeValue");
return execution.execute(request, body);
})
.build();
}
}
RestTemplate
個々のアクセス時にheaderを追加するなら下記のような感じ。
RestTemplate client = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("my-header", "value");
HttpEntity<String> request = new HttpEntity<>(headers);
client.exchange("http://localhost:8081/a", HttpMethod.GET, request, String.class);