java – 如何使用注释自动连接RestTemplate
发布时间:2020-12-14 05:18:32 所属栏目:Java 来源:网络整理
导读:当我尝试自动连接 Spring RestTemplate时,我收到以下错误: 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖关系的类型为[org.springframework.web.client.RestTemplate]的符合条件的bean:至少有一个bean符合此依
当我尝试自动连接
Spring RestTemplate时,我收到以下错误:
嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖关系的类型为[org.springframework.web.client.RestTemplate]的符合条件的bean:至少有一个bean符合此依赖关系的自动连线候选. 在注释驱动的环境中使用Spring 4. 我的调度程序servlet配置如下: <context:component-scan base-package="in.myproject" /> <mvc:default-servlet-handler /> <mvc:annotation-driven /> <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/> 我正在尝试自动连接RestTemplate的课程如下: @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); } } 解决方法
您可以将下面的方法添加到您的类中,以提供RestTemplate的默认实现:
@Bean public RestTemplate restTemplate() { return new RestTemplate(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |