mee*_*rma 52 java spring spring-mvc 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)
dus*_*ltz 100
如果RestTemplate
未定义a,您将看到错误
考虑在配置中定义类型为'org.springframework.web.client.RestTemplate'的bean.
要么
找不到[org.springframework.web.client.RestTemplate]类型的限定bean
如何定义RestTemplate
via注释
取决于您使用的技术以及哪些版本将影响您RestTemplate
在@Configuration
班级中定义的方式.
Spring> = 4没有Spring Boot
只需定义一个@Bean
:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
Run Code Online (Sandbox Code Playgroud)
Spring Boot <= 1.3
无需定义一个,Spring Boot会自动为您定义一个.
Spring Boot> = 1.4
Spring Boot不再自动定义RestTemplate
,而是定义一个RestTemplateBuilder
允许您更多地控制RestTemplate
创建的内容.您可以RestTemplateBuilder
在@Bean
方法中注入作为参数来创建RestTemplate
:
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
// Do any additional configuration here
return builder.build();
}
Run Code Online (Sandbox Code Playgroud)
在课堂上使用它
@Autowired
private RestTemplate restTemplate;
Run Code Online (Sandbox Code Playgroud)
要么
@Inject
private RestTemplate restTemplate;
Run Code Online (Sandbox Code Playgroud)
eay*_*kin 19
您可以将以下方法添加到您的类中,以提供RestTemplate的默认实现:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
Run Code Online (Sandbox Code Playgroud)
小智 11
如果您使用Spring Boot 1.4.0或更高版本作为注释驱动的基础,则Spring不提供单个自动配置的RestTemplate bean.从他们的文件:
https://docs.spring.io/spring-boot/docs/1.4.0.RELEASE/reference/html/boot-features-restclient.html
如果需要从应用程序调用远程REST服务,可以使用Spring Framework的RestTemplate类.由于RestTemplate实例在使用之前通常需要进行自定义,因此Spring Boot不提供任何单个自动配置的RestTemplate bean.它,然而,自动配置,可用于在需要时创建RestTemplate实例一RestTemplateBuilder.自动配置的RestTemplateBuilder将确保将合理的HttpMessageConverters应用于RestTemplate实例.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateClient {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Run Code Online (Sandbox Code Playgroud)
@Configuration
在 RestTemplateSOMENAME 中添加扩展 RestTemplate 类的注释。
@Configuration
public class RestTemplateClass extends RestTemplate {
}
Run Code Online (Sandbox Code Playgroud)
然后在您的控制器类中,您可以使用 Autowired 注释,如下所示。
@Autowired
RestTemplateClass restTemplate;
Run Code Online (Sandbox Code Playgroud)
@Autowired
private RestOperations restTemplate;
Run Code Online (Sandbox Code Playgroud)
您只能自动装配接口和实现。
归档时间: |
|
查看次数: |
94100 次 |
最近记录: |