Skip to content
Advertisement

Springboot property typemismatch

I have a common issue and none of all the other similar questions on the forum helped me sofar. Pls bear with me, im still learning.

l have a Spring boot app.

@RequiredArgsConstructor
@SpringBootApplication
public class ConsoleApp implements CommandLineRunner {

    @Value("${numberOfDocs:10}")
    private int numberOfDocuments;
    @Value("${filePath:testdoc-al.pdf}")
    private String filePath;

Unfortunately the first property that I declare doesn’t work due to type mismatch from String to int. The other works just fine. my application.properties looks like this:

#file path.
filePath=

#number of documents
numberOfDocs=

my stacktrace looks like this:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ConsoleApp': Unsatisfied dependency expressed through field 'numberOfDocuments'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: ""
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:598) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1404) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:860) ~[spring-beans-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.1.16.RELEASE.jar:5.1.16.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) ~[spring-boot-2.1.15.RELEASE.jar:2.1.15.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) ~[spring-boot-2.1.15.RELEASE.jar:2.1.15.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-2.1.15.RELEASE.jar:2.1.15.RELEASE]
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140) ~[spring-boot-2.1.15.RELEASE.jar:2.1.15.RELEASE]
at ConsoleApp.main(ConsoleApp.java:36) ~[main/:na]

What am I missing here? Why is my int property seen as String in application.property ? Do i need to cast int for it to work?

Advertisement

Answer

The issue is with the initialization of numberOfDocs property in application.properties file.

You have initilized numbeOfDocs as ""(empty string) and spring is trying to convert this empty string to integer, because you declared numberOfDocuments as int variable.

Your error will get fixed by one of thre ways

  1. Change numberOfDocuments data type to String
  2. Initilize numberOfDoc property with valid integer
  3. Just remove numberOfDoc property from application.properties. Meaning don’t initialize it so it will take default value 10
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement