java - 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 才能工作吗?
解决方案
numberOfDocs
问题在于application.properties 文件中属性的初始化。
您已初始化numbeOfDocs
为""
(empty string) 并且 spring 正在尝试将此空字符串转换为整数,因为您声明numberOfDocuments
为 int 变量。
您的错误将通过以下三种方式之一得到修复
numberOfDocuments
将数据类型更改为String
numberOfDoc
使用有效整数初始化属性- 只需
numberOfDoc
从 application.properties 中删除属性。意思是不要初始化它,所以它将采用默认值 10
推荐阅读
- sql - 从 SQL Server 到 Oracle 的转换
- r - R - 使用 dplyr、DT 和 Shiny 在地图中创建数据表
- bootstrap-4 - JHipster - 如何覆盖 Bootstrap 样式?
- php - PHPSpreadsheet并写入大量小数位
- python - 在 Python 中迭代 spark 数据框中的记录
- javascript - 使用条件将/替换运算符添加到字符串的开头
- sql - SQL - 在另一个表中复制列 // 在树中查找家庭关系
- java - 线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:java.lang.String.charAt(String.java:658) 处的 0
- cmake - 如何在 CMake 中跨多个目标共享属性?
- android - 语言解析策略与官方文档不一致