Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The method of Spring RestTemplate using interceptor to configure HTTP request Header

Shulou Source: shulou.com Published: 2022-06-02 10:13:47 10月13日 Update

This article mainly explains "Spring RestTemplate uses interceptor to configure HTTP request Header". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Spring RestTemplate uses interceptor to configure HTTP request Header".

Spring RestTemplate is often used as a client to send various requests to Restful API, and you may have encountered this requirement, and many requests require similar or the same Http Header. If you populate Header into HttpEntity/RequestEntity before each request, this code can be very redundant.

Spring provides a ClientHttpRequestInterceptor interface to intercept requests and modify requests or enhance corresponding information before they are sent to the server. Here is a simple example:

It is not necessary to implement the ClientHttpRequestInterceptor interface @ Componentpublic class ActionTrackInterceptor implements ClientHttpRequestInterceptor {@ Autowired ActionIdGenerator actionIdGenerator; @ Override public ClientHttpResponse intercept (HttpRequest request, byte [] body, ClientHttpRequestExecution execution) throws IOException {HttpHeaders headers = request.getHeaders (); / / add custom fields headers.add ("actionId", actionIdGenerator.generate ()); / / ensure that the request continues to be executed return execution.execute (request, body) }} add a custom interceptor to the RestTemplate instance @ Configurationpublic class ClientConfig {/ / injection interceptor. The interceptor can also not declare as Bean, but directly create a new instance @ Autowired ActionTrackInterceptor actionTrackInterceptor; / / declare it as Bean here to facilitate the use of the same instance @ Bean public RestTemplate restTemplate () {RestTemplate restTemplate = new RestTemplate () in the application; / / add custom ClientHttpRequestInterceptor to RestTemplate, and add multiple restTemplate.setInterceptors (Collections.singletonList (actionTrackInterceptor)); return restTemplate;}}

The previous work has been completed, and now if you use this RestTemplate instance to send a request, you will have the field "actionId" in the Header. Of course, you can configure more general fields such as Accept, Content-Type and so on.

/ / client code restTemplate.getForObject (SERVER_URL, Example.class) / / server code / / if the server is also implemented in Spring RestController/MVC, the previously added actionId field can be obtained by using the @ RequestHeader annotation. @ RequestMapping (value = "/ example") public Example example (@ RequestHeader ("actionId") String actionId) {/ / business logic} so far, I believe you have a better understanding of "Spring RestTemplate uses interceptor to configure HTTP request Header". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Interceptor method configuration field instance code service content client client interface more learning practical deeper similar same necessary business example Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Huawei MariaDB NVidia MySQL