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();
}

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...