opentracing-contrib / java-spring-web

OpenTracing Spring Web instrumentation
Apache License 2.0
108 stars 59 forks source link

RestTemplate can't be autowired #74

Closed abhimanyuseth closed 6 years ago

abhimanyuseth commented 6 years ago

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.

abhimanyuseth commented 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)

geoand commented 6 years ago

@abhimanyuseth Have you tried to simply declare a RestTemplate bean like so:

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}
abhimanyuseth commented 6 years ago

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?

geoand commented 6 years ago

@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

abhimanyuseth commented 6 years ago

Thank you!

geoand commented 6 years ago

You are welcome!