Closed abhimanyuseth closed 6 years ago
If I add my own configuration bean to Autowire restTemplate, I'm unable to load the TracingRestTemplateInterceptor at runtime...
@Bean public RestTemplate restTemplate(RestTemplateBuilder builder, Tracer tracer) { RestTemplate restTemplate = builder.build(); restTemplate.setInterceptors(Collections.singletonList(new TracingRestTemplateInterceptor(tracer))); return restTemplate; }
Caused by: java.lang.ClassNotFoundException: io.opentracing.contrib.spring.web.client.TracingRestTemplateInterceptor at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:148) at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
@abhimanyuseth Have you tried to simply declare a RestTemplate
bean like so:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
Yeah, I did. But from what I understand, the RestTemplate object needs to have some interceptors set where we can trace all REST API requests. If I create the bean myself, then no tracing is done unless I write my own interceptor.
Please correct me if I'm wrong. Using opentracing-contrib, How should I get all RestController calls traced if I'm creating the bean myself?
@abhimanyuseth When Opentracing's auto-configuration is applied, then the presence of the a RestTemplate
type bean is enough to have all the necessary interceptors applied.
See this autoconfiguration class
Thank you!
You are welcome!
With springBootVersion = '2.0.0.RELEASE' I'm trying to use autoconfiguration, and using @Autowired RestTemplate restTemplate;
This fails with the following error:
APPLICATION FAILED TO START
Description:
Field restTemplate in oracle.igcs.tracer.controller.HelloController required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.