1

I got the following error message when starting a spring boot/cloud application which gets some properties from 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

Here's my main class:

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

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

And the configuration property class:

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

    private int number;

    public int getNumber() {
        return number;
    }

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

}

The application.yml file is:

server:
  port: 8100

spring:
 application:
    name: demo

And the bootstrap.yml file:

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

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

Finally the related part of maven pom.xml file:

<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>
Tonny Tc
  • 852
  • 1
  • 12
  • 37
  • Any reason you think this has to do with spring-cloud-consul? – spencergibb Jun 26 '17 at 17:27
  • If without consul's key-value configuration, viz `config:number:5`, the app works fine for int type. And if the `number` is a string type, it also works fine. – Tonny Tc Jun 26 '17 at 23:56

1 Answers1

0

I found the problem is because PropertySourceBootstrapConfiguration cannot locate property source from consul in some case. If PropertySourceBootstrapConfiguration is properly initiated during the booting up, there's no such exception and the app works as expected.

Tonny Tc
  • 852
  • 1
  • 12
  • 37