java 注释 连接_关于Java:如何使用注释自动连接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.

在注释驱动的环境中使用Spring4。

我的调度程序servlet 配置如下:

我试图自动连接RestTemplate的类如下:

@Service("httpService")

public class HttpServiceImpl implements HttpService {

@Autowired

private RestTemplate restTemplate;

@Override

public void sendUserId(String userId){

MultiValueMap map = new LinkedMultiValueMap<>();

map.add("userId", userId);

map.add("secretKey","kbhyutu7576465duyfy");

restTemplate.postForObject("http://localhost:8081/api/user", map, null);

}

}

您是否可以确认在HttpServiceImpl中要导入的是org.springframework.web.client.RestTemplate类,而不是其他的RestTemplate实现?

我导入相同的org.springframework.web.client.RestTemplate

您的配置文件可能没有被读取,请在其中添加一些其他bean并查看它是否在上下文中注册,如果没有,您会得到答案

我在dispatcher-servlet.xml中声明了bean(),而不是applicationContext.xml,现在可以正常工作了。

如果未定义RestTemplate,则会看到错误

Consider defining a bean of type

'org.springframework.web.client.RestTemplate' in your configuration.

要么

No qualifying bean of type

[org.springframework.web.client.RestTemplate] found

如何通过注释定义RestTemplate

根据您使用的技术以及哪个版本会影响您在@Configuration类中定义RestTemplate的方式。

没有Spring Boot的Spring> = 4

只需定义一个@Bean:

@Bean

public RestTemplate restTemplate() {

return new RestTemplate();

}

春季靴<= 1.3

无需定义一个,Spring Boot会自动为您定义一个。

春季靴> = 1.4

Spring Boot不再自动定义RestTemplate,而是定义了RestTemplateBuilder,使您可以更好地控制要创建的RestTemplate。您可以在@Bean方法中注入RestTemplateBuilder作为参数来创建RestTemplate:

@Bean

public RestTemplate restTemplate(RestTemplateBuilder builder) {

// Do any additional configuration here

return builder.build();

}

在课堂上使用它

@Autowired

private RestTemplate restTemplate;

要么

@Inject

private RestTemplate restTemplate;

问题是有关在类中自动装配RestTemplate的问题,因此无需将Bean放入带@Configuration注释的类中。 可以将其直接放在类中(请参阅@eaykin答案)

当然,您可以在@Component类中声明它(毕竟,@Configuration是@Component的元注释),但首选@Configuration。 见stackoverflow.com/a/28002891/2429176

我不能对此表示足够的赞同。

嗨,如果我的课程是功能接口的实现,该怎么办? 如何使restTemplate对象自动连线? 存在另一个函数会引发错误!

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

@Bean

public RestTemplate restTemplate() {

return new RestTemplate();

}

+1表示您可以将其添加到类中,而无需将其放在@Configuration带注释的类中

我将其放在我的一项服务中,它说Bean创建循环错误,因此我需要采用另一种方式进行配置注释

如果您使用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

If you need to call remote REST services from your application, you can use Spring Framework’s RestTemplate class. Since RestTemplate instances often need to be customized before being used, Spring Boot does not provide any single auto-configured RestTemplate bean. It does, however, auto-configure a RestTemplateBuilder which can be used to create RestTemplate instances when needed. The auto-configured RestTemplateBuilder will ensure that sensible HttpMessageConverters are applied to RestTemplate instances.

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

}

}

@Autowired

private RestOperations restTemplate;

您只能将接口与实现自动关联。

RestOperations是一个接口docs.spring.io/spring/docs/current/javadoc-api/org/

那就是他所说的。 如果接口具有一种实现,则可以对其进行自动布线。

在RestTemplateSOMENAME中添加@Configuration批注,以扩展RestTemplate类。

@Configuration

public class RestTemplateClass extends RestTemplate {

}

然后,在您的控制器类中,可以按如下方式使用自动装配注释。

@Autowired

RestTemplateClass restTemplate;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值