使用spring-cloud-consul为int属性抛出ConverterNotFoundException

时间:2017-06-26 01:43:48

标签: spring spring-boot spring-cloud consul spring-cloud-consul

启动spring boot / cloud应用程序时,我收到以下错误消息,该应用程序从consul获得了一些属性:

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target demo.config.Config@5b4954b2 failed:

    Property: config.number
    Value: ${config.number:5}
    Reason: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'number'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [int]

Action:

Update your application's configuration

这是我的主要课程:

@SpringBootApplication
@WebAppConfiguration
@EnableDiscoveryClient
@EnableConfigurationProperties
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

配置属性类:

@Component
@ConfigurationProperties(prefix="config")
public class Config {   

    private int number;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

}

application.yml文件是:

server:
  port: 8100

spring:
 application:
    name: demo

和bootstrap.yml文件:

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
    config:
      enabled: true
      prefix: config 

config:
  number: ${config.number:5}

最后是maven pom.xml文件的相关部分:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    <cucumber.version>1.2.5</cucumber.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

1 个答案:

答案 0 :(得分:0)

我发现问题是因为List < T >在某些情况下无法从领事找到属性来源。如果在启动过程中正确启动了PropertySourceBootstrapConfiguration,则没有此类异常且应用按预期工作。