我正在尝试记录 Resttemplate 请求和响应以及它working我的参考是这个https://www.baeldung.com/spring-resttemplate-logging https://www.baeldung.com/spring-resttemplate-logging and https://medium.com/javarevisited/spring-resttemplate-request-response-logging-f021be66c2c0 https://medium.com/javarevisited/spring-resttemplate-request-response-logging-f021be66c2c0但问题是,它得到了它所有端点。我想要一个c仅某些端点. https://www.baeldung.com/rest-template https://www.baeldung.com/rest-template.如果您有建议,请分享。我已经寻找解决方案好几天了。
我可以想到的两种方式:
- 实现您想要登录拦截器的 URL(或模式)
- 配置2
RestTemplate
一个有日志记录的实例,一个没有日志记录的实例,并决定在哪里使用哪个。您可以使用专用的@Qualifier
为了这。
@Target({ElementType.FIELD,
ElementType.METHOD,
ElementType.TYPE,
ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Logged {}
您现在可以使用此限定注释来创建 2 个 bean 并将其放在@Autowired
字段或构造函数参数来决定注入哪个。
@Bean
@Primary
public RestTemplate restTemplate(RestTemplateBuilder rtb) {
return rtb.build();
}
@Bean
@Logged
public RestTemplate restTemplate(RestTemplateBuilder rtb) {
return rtb.interceptors(new RequestLoggingInterceptor()).build();
}
现在您的代码将使用@Primary
凡有一个地方RestTemplate
需要。如果您想使用日志记录,请使用以下命令显式注释该位置@Logged
.
@Service
public class MyService {
private final RestTemplate rest;
public MyService(@Logged RestTemplate rest) {
this.rest=rest;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)