相关疑难解决方法(0)

如何使用注释自动装配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"/>
Run Code Online (Sandbox Code Playgroud)

我尝试自动装配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);


    }
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc resttemplate

52
推荐指数
6
解决办法
9万
查看次数

标签 统计

java ×1

resttemplate ×1

spring ×1

spring-mvc ×1