如何在Spring Boot的请求中设置添加新的标题

时间:2018-12-20 20:48:53

标签: java spring spring-boot

嗨,我正在使用Spring Boot 2.1.1,并且已经编写了REST端点。如果有请求,则需要对其进行验证,基于验证,我需要添加一个新的Request标头作为“ NEW_SES_VAL_ID”,并将值添加为“ 12345”。我还编写了拦截器,但无法添加到请求标头中。请帮助有关如何在Spring Boot中的Interceptor中的每个传入请求中添加新的Request标头。

1 个答案:

答案 0 :(得分:0)

您可以创建扩展ClientHttpRequestInterceptor

的拦截器

类似这样的东西:

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
}

希望有帮助。