简体   繁体   中英

How to set add a new Header in Request in Spring Boot

Hi I am using Spring Boot 2.1.1 and I have written REST end points. If a request comes, it needs to be validated, based upon validation, I need add to add a new Request header as "NEW_SES_VAL_ID" and value as "12345". I have written interceptor also but I am unable to add to Request Header. Please help about how to add a new Request header in every incoming request in Interceptor in Spring Boot.

You can create an interceptor extending ClientHttpRequestInterceptor

Something like this:

public class MyCustomInterceptor implements ClientHttpRequestInterceptor {

    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        // some code ...

        HttpHeaders headers = request.getHeaders();
        headers.add("MyHeader", "headerValue");

       // more code ...

       return execution.execute(request, body);
    }

   //Rest of the class
}

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM