微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 如何使用注释自动连接RestTemplate

当我尝试自动连接 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();
}

原文地址:https://www.jb51.cc/java/122899.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐