首页 > 解决方案 > Springboot 属性类型不匹配

问题描述

我有一个共同的问题,到目前为止,论坛上所有其他类似的问题都没有帮助我。请多多包涵,我还在学习。

我有一个 Spring Boot 应用程序。

@RequiredArgsConstructor
@SpringBootApplication
public class ConsoleApp implements CommandLineRunner {

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

不幸的是,由于从 String 到 int 的类型不匹配,我声明的第一个属性不起作用。其他工作正常。我的 application.properties 看起来像这样:

#file path.
filePath=

#number of documents
numberOfDocs=

我的堆栈跟踪看起来像这样:

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]

我在这里想念什么?为什么我的 int 属性在 application.property 中被视为字符串?我需要强制转换 int 才能工作吗?

标签: javaspringspring-boottype-mismatch

解决方案


numberOfDocs问题在于application.properties 文件中属性的初始化。

您已初始化numbeOfDocs""(empty string) 并且 spring 正在尝试将此空字符串转换为整数,因为您声明numberOfDocuments为 int 变量。

您的错误将通过以下三种方式之一得到修复

  1. numberOfDocuments将数据类型更改为String
  2. numberOfDoc使用有效整数初始化属性
  3. 只需numberOfDoc从 application.properties 中删除属性。意思是不要初始化它,所以它将采用默认值 10

推荐阅读