выбросить ConverterNotFoundException, используя spring-cloud-consul для свойства int

Я получил следующее сообщение об ошибке при запуске весеннего загрузочного / облачного приложения, которое получает некоторые свойства от консула:

***************************
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>

person Tonny Tc    schedule 26.06.2017    source источник
comment
Есть ли причина, по которой вы думаете, что это связано с весенним облаком-консулом?   -  person spencergibb    schedule 26.06.2017
comment
Если без конфигурации "ключ-значение" консула, а именно config:number:5, приложение отлично работает с типом int. И если number является строковым типом, он также работает нормально.   -  person Tonny Tc    schedule 27.06.2017


Ответы (1)


Я обнаружил, что проблема в том, что PropertySourceBootstrapConfiguration в некоторых случаях не может найти источник собственности от консула. Если PropertySourceBootstrapConfiguration правильно инициирован во время загрузки, такого исключения нет, и приложение работает должным образом.

person Tonny Tc    schedule 28.06.2017