Set default content type header of Spring RestTemplate

前端 未结 2 869
被撕碎了的回忆
被撕碎了的回忆 2021-02-18 23:55

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

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-19 00:30

    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;
            }
        }
    

提交回复
热议问题