Java – how to automatically connect resttemplate using annotations
•
Java
When I try to automatically connect to spring resttemplate, I receive the following error:
The nested exception is org springframework. beans. factory. Nosuchbeandefinitionexception: no eligible beans of type [org. Springframework. Web. Client. Resttemplate] of dependency found: at least one bean meets the automatic connection candidate of this dependency
Using spring 4.0 in an annotation driven environment
My scheduler servlet is configured as follows:
<context:component-scan base-package="in.myproject" /> <mvc:default-servlet-handler /> <mvc:annotation-driven /> <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
The courses I'm trying to automatically connect to resttemplate are as follows:
@Service("httpService") public class HttpServiceImpl implements HttpService { @Autowired private RestTemplate restTemplate; @Override public void sendUserId(String userId){ MultiValueMap<String,String> map = new LinkedMultiValueMap<>(); map.add("userId",userId); map.add("secretKey","kbhyutu7576465duyfy"); restTemplate.postForObject("http://localhost:8081/api/user",map,null); } }
Solution
You can add the following methods to your class to provide the default implementation of resttemplate:
@Bean public RestTemplate restTemplate() { return new RestTemplate(); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码