如何使用注释自动装配RestTemplate

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

如何定义RestTemplatevia注释

取决于您使用的技术以及哪些版本将影响您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)

  • 问题是关于在类中自动装配 RestTemplate,因此无需将 bean 放入“@Configuration”注释类中。可以直接放在类中(参见@eaykin回答) (2认同)

eay*_*kin 19

您可以将以下方法添加到您的类中,以提供RestTemplate的默认实现:

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}
Run Code Online (Sandbox Code Playgroud)

  • +1 提到您可以将其添加到类中,无需将其放入@Configuration 注释类中 (3认同)
  • 我把它放在我的一个服务中,它说 bean 创建循环错误,所以我需要使用配置注释的另一种方式 (2认同)

小智 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实例.


Kum*_*mar 7

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)


Abh*_*hal 7

@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)


ris*_*p89 2

@Autowired
private RestOperations restTemplate;
Run Code Online (Sandbox Code Playgroud)

您只能自动装配接口和实现。