包含 Eureka 时,自动装配 RestTemplate 会导致 "argument type mismatch"

Autowiring RestTemplate causes "argument type mismatch" when including Eureka

添加后

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

我的依赖项出现以下异常

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [example.nagios.notificationmanager.core.nagiosapi.NagiosAPIService]: Illegal arguments for constructor; nested exception is java.lang.IllegalArgumentException: argument type mismatch
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:158)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:267)
    ... 24 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 26 more

因此使用以下构造函数创建服务失败

@Autowired
public NagiosAPIService(@Value("${nagios.state.uri}") String nagiosStateUri,
    @Qualifier("systemAuthorized") RestTemplate restTemplate, NagiosCheckService nagiosCheckService) {

    this.nagiosStateUri = nagiosStateUri;
    this.restTemplate = restTemplate;
    this.nagiosCheckService = nagiosCheckService;
}

我一个接一个地删除了参数,导致错误的是 RestTemplate。

在我的依赖项中没有 spring-cloud-starter-eureka 一切正常。 谁能告诉我为什么 eureka 会导致这个错误?

我认为只是有一些(可选,但如果您使用该启动器则默认情况下处于活动状态)自动配置来监视指标的其余模板使用情况。您可能仍然可以自动装配 RestOperations,或设置 spring.aop.proxy-target-class=true 使其按原样工作。