如何使用注释自动连接 RestTemplate
当我尝试自动连接Spring RestTemplate时,我收到以下错误:
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.client.RestTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
在注释驱动的环境中使用 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);
}
}