Spring 启动无法访问 ClientHttpRequestInterceptor 内的@Value
Spring boot cannot access @Value inside ClientHttpRequestInterceptor
我正在使用 spring-boot-1.3.3.I 想拦截 Rest 模板,我可以拦截它,但我无法访问 application.properties.It 总是 returns空。
package com.sample.filter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class HeaderInterceptor implements ClientHttpRequestInterceptor{
private static final String CLIENT_HEADER = "x-client";
@Value("${clientHeader}")
private String ClientHeader;
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = httpRequest.getHeaders();
headers.add(CLIENT_HEADER, ClientHeader);
return execution.execute(httpRequest, body);
}
}
我总是得到 "clientHeader" 键为空。
如有任何帮助,我们将不胜感激。
我看到您在评论中提到您在代码中通过 new
关键字自行创建拦截器。为了使用 HeaderInterceptor 的 spring 上下文实例,您需要在您的代码中 autowire 它。只有这样,它才会具有 spring 托管属性的可见性。
您可以为拦截器配置@Bean,这将确保@Autowired 字段确实是自动装配的,然后使用它们来配置客户端。
之前:
client.setInterceptors(new ClientInterceptor[] {new CustomInterceptor()});
之后:
@Bean
public CustomInterceptor customInterceptor() {
return new CustomInterceptor();
}
//in the client construction, set the interceptor as below.
client.setInterceptors(new ClientInterceptor[] {customInterceptor()});
我正在使用 spring-boot-1.3.3.I 想拦截 Rest 模板,我可以拦截它,但我无法访问 application.properties.It 总是 returns空。
package com.sample.filter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class HeaderInterceptor implements ClientHttpRequestInterceptor{
private static final String CLIENT_HEADER = "x-client";
@Value("${clientHeader}")
private String ClientHeader;
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = httpRequest.getHeaders();
headers.add(CLIENT_HEADER, ClientHeader);
return execution.execute(httpRequest, body);
}
}
我总是得到 "clientHeader" 键为空。
如有任何帮助,我们将不胜感激。
我看到您在评论中提到您在代码中通过 new
关键字自行创建拦截器。为了使用 HeaderInterceptor 的 spring 上下文实例,您需要在您的代码中 autowire 它。只有这样,它才会具有 spring 托管属性的可见性。
您可以为拦截器配置@Bean,这将确保@Autowired 字段确实是自动装配的,然后使用它们来配置客户端。
之前:
client.setInterceptors(new ClientInterceptor[] {new CustomInterceptor()});
之后:
@Bean
public CustomInterceptor customInterceptor() {
return new CustomInterceptor();
}
//in the client construction, set the interceptor as below.
client.setInterceptors(new ClientInterceptor[] {customInterceptor()});