Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default content type header of Spring RestTemplate

Tags:

I'm currently using an OAuth2RestOperations that extends the Spring RestTemplate and I would like to specify the content type header.

The only thing I've managed to do was to explicitly set my header during the request:

public String getResult() {
    String result = myRestTemplate.exchange(uri, HttpMethod.GET, generateJsonHeader(), String.class).getBody();
}

private HttpEntity<String> generateJsonHeader() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    return new HttpEntity<>("parameters", headers);
}

But it would actually be great to be able to set that once and for all during the bean initialization, and directly use the getforObject method instead of exchange.

like image 369
Rlarroque Avatar asked Apr 24 '17 14:04

Rlarroque


People also ask

How do you set headers in Spring RestTemplate?

RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers. setAccept(Collections. singletonList(MediaType. APPLICATION_JSON)); HttpEntity<String> httpEntity = new HttpEntity<>("some body", headers); restTemplate.

How do you set HttpEntity headers?

To answer this question, you can use one of the exchange methods that accepts an HttpEntity for which you can also set the HttpHeaders. (You can also sepecify the HTTP method you want to use). RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders();

What is difference between getForObject and getForEntity?

For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.

How do you set HttpEntity in RestTemplate?

httpEntity = new HttpEntity<Object>(requestHeaders); MyModel[] models = restTemplate. exchange("/api/url", HttpMethod. GET, httpEntity, MyModel[].


2 Answers

First you have to create request interceptor:

public class JsonMimeReqInterceptor implements ClientHttpRequestInterceptor {

  @Override
  public ClientHttpResponse intercept(HttpRequest request, byte[] body,
        ClientHttpRequestExecution execution) throws IOException {
    HttpHeaders headers = request.getHeaders();
    headers.add("Accept", MediaType.APPLICATION_JSON);
    return execution.execute(request, body);
  }
}

... and then you have rest template creation code which uses above interceptor:

@Configuration
public class MyAppConfig {

  @Bean
  public RestTemplate restTemplate() {
      RestTemplate template = new RestTemplate(clientHttpRequestFactory());
      //magic happens below:
      template.setInterceptors(Collections.singletonList(new JsonMimeReqInterceptor()));
      return restTemplate;
  }
}

You could subclass RestTemplate if you were to have some other specialised or universal REST templates in your application.

like image 96
diginoise Avatar answered Sep 18 '22 19:09

diginoise


If you're using Spring Boot, you can just

@Configuration
    public class RestConfig {
        @Bean
        public RestTemplate getRestTemplate() {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.setInterceptors(Collections.singletonList(new HttpHeaderInterceptor("Accept",
                    MediaType.APPLICATION_JSON.toString())));
            return restTemplate;
        }
    }
like image 43
maaw Avatar answered Sep 21 '22 19:09

maaw