Springboot 属性 类型不匹配

Springboot property typemismatch

我有一个常见问题,到目前为止,论坛上的所有其他类似问题 none 对我都有帮助。请耐心等待,我还在学习中。

我有一个 Spring 启动应用程序。

@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[=13=](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 中被视为 String?我需要强制转换 int 才能工作吗?

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

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

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

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