Custom Request Header (custom HTTP headers) when calling the rest service using spring resttemplate)

Source: Internet
Author: User

In Spring 3.0, you can use the HttpEntity object to customize request header information, such:

private static final String APPLICATION_PDF = "application/pdf"; RestTemplate restTemplate = new RestTemplate(); @Testpublic void acceptHeaderUsingHttpEntity() throws Exception {  HttpHeaders headers = new HttpHeaders();  headers.setAccept(singletonList(MediaType.valueOf(APPLICATION_PDF)));   ResponseEntity<byte[]> response = restTemplate.exchange("http://example.com/file/123",      GET,      new HttpEntity<byte[]>(headers),      byte[].class);   String responseText = PdfTextExtractor.getTextFromPage(new PdfReader(response.getBody()), 1);  assertEquals("Some text in PDF file", responseText);}

In Spring 3.1, there is a more powerful alternative interface ClientHttpRequestInterceptor, which has only one method:Intercept (HttpRequest
Request, byte [] body, ClientHttpRequestExecution execution ),
The following is an example:

private static final String APPLICATION_PDF = "application/pdf"; RestTemplate restTemplate = new RestTemplate(); @Testpublic void acceptHeaderUsingHttpRequestInterceptors() throws Exception {  ClientHttpRequestInterceptor acceptHeaderPdf = new AcceptHeaderHttpRequestInterceptor(      APPLICATION_PDF);   restTemplate.setInterceptors(singletonList(acceptHeaderPdf));   byte[] response = restTemplate.getForObject("http://example.com/file/123", byte[].class);   String responseText = PdfTextExtractor.getTextFromPage(new PdfReader(response), 1);  assertEquals("Some text in PDF file", responseText);} class AcceptHeaderHttpRequestInterceptor implements ClientHttpRequestInterceptor {  private final String headerValue;   public AcceptHeaderHttpRequestInterceptor(String headerValue) {    this.headerValue = headerValue;  }   @Override  public ClientHttpResponse intercept(HttpRequest request, byte[] body,      ClientHttpRequestExecution execution) throws IOException {     HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);    requestWrapper.getHeaders().setAccept(singletonList(MediaType.valueOf(headerValue)));     return execution.execute(requestWrapper, body);  }}


Original article: http://svenfila.wordpress.com/2012/01/05/resttemplate-with-custom-http-headers/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.