在springboot中未调用ClientHttpRequestInterceptor [英] ClientHttpRequestInterceptor not called in springboot
问题描述
我正在尝试使用ClientHttpRequestInterceptor将日志添加到我的应用程序中.我的拦截器没有被调用.不知道这里出了什么问题-
I am trying to add logging to my application using ClientHttpRequestInterceptor.My interceptor is not being called. Not sure what is going wrong here -
这是我的代码-
@Component
@Slf4j
public final class RestTemplateInterceptor implements ClientHttpRequestInterceptor {
protected static final LoggingAspect aspect = new LoggingAspect();
private final RequestContext requestContext;
private boolean logResponseBody = true;
public RestTemplateInterceptor(RequestContext requestContext) {
this.requestContext = requestContext;
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
populateHeader(request);
traceRequest(request, body);
ClientHttpResponse response = clientHttpRequestExecution.execute(request,body);
traceResponse(response);
return response;
}
private void populateHeader(HttpRequest request) {
final HttpHeaders headers = request.getHeaders();
// Propagate TAM headers
headers.add("iv-user", requestContext.getUser());
headers.add("MessageId", requestContext.getMessageId());
headers.add("CorrelationId", requestContext.getConversationId());
headers.add("BusinessId", requestContext.getBusinessId());
headers.add("ApplicationName", requestContext.getSourceSystem());
headers.add("iv-groups", requestContext.getGroups());
headers.add("MessageDateTime", requestContext.getSourceTimestamp());
}
...................
这是我的配置文件
@Configuration
public class RestTemplateConfig {
/**
* initialise restTemplate
*
* @param restTemplateInterceptor autowired in RestTemplateInterceptor
* @return
*/
@Bean
public RestTemplate restTemplate(ClientHttpRequestInterceptor restTemplateInterceptor, ObjectMapper objectMapper) {
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
if (CollectionUtils.isEmpty(interceptors)) {
interceptors = new ArrayList<>();
}
interceptors.add(restTemplateInterceptor);
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
}
这是我的WebMVC文件
Here is my WebMVC file
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public WebMvcConfigurer webAuthentication() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//registry.addInterceptor(myInterceptor());
registry.addInterceptor(new MVCLoggingInterceptor()).addPathPatterns("/api/**");
registry.addInterceptor(new WebAuthentication()).addPathPatterns("/api/**/");
}
};
}
}
这是我的申请文件
@EnableAsync
@EnableScheduling
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
public class XManagementApplication {
public static void main(String[] args) {
SpringApplication.run(XManagementApplication.class, args);
}
}
有人可以告诉我为什么在尝试调用任何API时未调用我的拦截器类
Can anybody tell why my interceptor class is not called when I try to call any API
有什么帮助吗?
推荐答案
-
我不太了解为什么要将RestTemplateInterceptor实例化为Bean.为什么不简单地在RestTemplateConfig.restTemplate()方法中实例化您的拦截器呢?
I don't really understand why you want to instantiate your RestTemplateInterceptor as a Bean. Why not simply instantiate your interceptor inside the method RestTemplateConfig.restTemplate() ?
@配置公共类RestTemplateConfig {
@Configuration public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
if (CollectionUtils.isEmpty(interceptors)) {
interceptors = new ArrayList<>();
}
interceptors.add(new RestTemplateInterceptor());
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
}
顺便说一句,为什么您需要将RequestContext传递给拦截器的构造函数?
Btw, why do you need to pass RequestContext to the constructor of your interceptor ?
这篇关于在springboot中未调用ClientHttpRequestInterceptor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!