无法从 Java Spring Boot 项目中的 application.yml 文件中读取用户定义类的列表

     2023-02-25     89

关键词:

【中文标题】无法从 Java Spring Boot 项目中的 application.yml 文件中读取用户定义类的列表【英文标题】:Unable to read list of user defined class from application.yml file in a Java Spring Boot project 【发布时间】:2021-02-13 02:45:43 【问题描述】:

团队您好,

我最近尝试从 Spring Boot 项目(版本 2.3.4)中的 application.yml 文件中读取内容。

最初,yml 文件中的所有属性都被读取为 null。

在多次清理和重建项目后,我可以读取除用户定义的类对象列表(下类中的 List)之外的所有属性,它仍然被读取为空。

我尝试了所有可能的解决方案,但对我没有任何效果。

能否请您检查并帮助我理解我在下面的代码中遗漏了什么,因为 List logComponents 的值仍然从 yml 文件中读取为 null?

谢谢你的期待!

配置 Java 类

@配置 @EnableConfigurationProperties @ConfigurationProperties 公共类 TestAPIConfiguration 私有字符串事件缓存; 私有字符串diskBasedCache; 私有列表 sendAllSMSto; 私人列表 sendAllEmailsto; //这个属性被读取为null,即使 //此属性的值存在于 yml 文件中。 私有列表 logComponents; @NotNull 私有字符串 selfURIPrefix; @NotNull 私有字符串investURIPrefix; @NotNull 私有字符串 ifaURIPrefix; 私有字符串 apiEnv; private final Joiner joiner = Joiner.on( "," ).skipNulls(); 私有静态最终记录器 LOGGER = LoggerFactory.getLogger(TestAPIConfiguration.class); @PostConstruct 公共无效 setSystemProperties() 尝试 System.setProperty(SystemConstants.EVENT_CACHE_PATH, eventCache); System.setProperty(SystemConstants.DISK_BASED_CACHE_PATH, diskBasedCache); System.setProperty(SystemConstants.REQUEST_LOGGING_FIELDS, JSONUtils.getObjectMapper().writeValueAsString(logComponents)); System.setProperty(SystemConstants.ENVIRONMENT_IDENTIFIER, apiEnv); System.setProperty(INVEST_URI_PREFIX,investURIPrefix); System.setProperty(IFA_URI_PREFIX, ifaURIPrefix); 如果(sendAllSMSto != null) System.setProperty(SEND_ALL_SMS_TO, joiner.join(sendAllSMSto)); 如果(sendAllEmailsto != null) System.setProperty(SystemConstants.SEND_ALL_EMAILS_TO, joiner.join(sendAllEmailsto)); 捕捉(异常se) LOGGER.error("配置设置错误:", se.getLocalizedMessage()); 公共字符串 getEventCache() 返回事件缓存; 公共无效setEventCache(字符串事件缓存) this.eventCache = eventCache; 公共字符串 getDiskBasedCache() 返回diskBasedCache; 公共无效setDiskBasedCache(字符串diskBasedCache) this.diskBasedCache = diskBasedCache; 公共列表 getSendAllSMSto() 返回发送所有短信; 公共无效 setSendAllSMSto(List sendAllSMSto) this.sendAllSMSto = sendAllSMSto; 公共列表 getSendAllEmailsto() 返回 sendAllEmailsto; 公共无效 setSendAllEmailsto(List sendAllEmailsto) this.sendAllEmailsto = sendAllEmailsto; 公共列表 getRequestLoggingFields() 返回日志组件; 公共无效 setRequestLoggingFields(List requestLoggingFields) this.logComponents = requestLoggingFields; 公共字符串 getSelfURIPrefix() 返回 selfURIPrefix; 公共无效 setSelfURIPrefix(字符串 selfURIPrefix) this.selfURIPrefix = selfURIPrefix; 公共字符串 getInvestURIPrefix() 返回投资URI前缀; 公共无效 setInvestURIPrefix(字符串投资URIPrefix) this.investURIPrefix = 投资URIPrefix; 公共字符串 getIfaURIPrefix() 返回 ifaURIPrefix; 公共无效 setIfaURIPrefix(字符串 ifaURIPrefix) this.ifaURIPrefix = ifaURIPrefix; 公共字符串 getApiEnv() 返回 apiEnv; 公共无效 setApiEnv(字符串 apiEnv) this.apiEnv = apiEnv;

LogComponent Java 类

@零件 公共类 LogComponent @NotNull 私有字符串头名; @NotNull 私有字符串会话密钥; @NotNull 私有字符串 logPrintKey; 公共字符串 getHeaderName() 返回标头名称; 公共字符串 getSessionKey() 返回会话密钥; 公共字符串 getLogPrintKey() 返回日志打印键;

application.yml 文件

调试:真 服务器: 端口:8080 apiEnv:UAT selfURIPrefix: "https://testurl.localhost.net" 投资URIPrefix:“https://testurl.mediaserver.net” ifaURIPrefix: "https://testurl.secondaryserver.net" 发送所有短信到: - “0000000000” 发送所有电子邮件至: - “abc@testmail.com” eventCache: "C:\\Users\\username\\project\\devnull\\eventcachepurchase.mdb" diskBasedCache: "C:\\Users\\username\\project\\devnull\\cache.mdb" 日志组件: - headerName:X-RT-REQUEST-TRACKER 会话密钥:不可用 logPrintKey:请求跟踪器 - 标题名称:X-RT-INX-DWD 会话密钥:IFX-PDR 日志打印键:PDR_NO - 标题名称:X-RT-IFA-ARN sessionKey:IRX-AXRN logPrintKey:AXR-CDODEEE

【问题讨论】:

【参考方案1】:

终于找到了解决办法。

我没有在 LogComponent 类中创建 setter 方法,因为没有将值分配给变量。

为所有字段添加setter后,此问题已解决。

【讨论】:

Spring Boot 项目中的 Gradle 无法识别路径中的 Java jdk

】SpringBoot项目中的Gradle无法识别路径中的Javajdk【英文标题】:GradleinSpringBootprojectnotrecognizingJavajdkinPath【发布时间】:2021-08-0801:45:01【问题描述】:我有一个gradle版本为7.0的SpringBoot应用程序。我不得不将我的openjdk16更改为openjdk1... 查看详情

Gradle 无法同步 Spring Boot 项目

】Gradle无法同步SpringBoot项目【英文标题】:GradlecannotsyncSpringBootproject【发布时间】:2020-04-0623:39:24【问题描述】:我正在尝试从intelij中的start.spring.io使用gradle导入新项目,但它无法下载任何依赖项或导入插件。我看到来源列表... 查看详情

从 Spring Boot 项目部署战争

...ll时出现以下异常嵌套异常是java.lang.IllegalArgumentException:无法解析值“classpath:endpoints-$env.p 查看详情

Thymeleaf 无法检测到 spring-boot 项目中的模板

】Thymeleaf无法检测到spring-boot项目中的模板【英文标题】:Thymeleafcannotdetecttemplatesinsidespring-bootproject【发布时间】:2017-08-1407:40:23【问题描述】:我的SpringBoot应用程序中有以下项目结构,我想在其中使用ThymeleafprojectName-Gradle-Modul... 查看详情

无法从类型转换(Java - Spring boot)

】无法从类型转换(Java-Springboot)【英文标题】:Failedtoconvertfromtype(Java-Springboot)【发布时间】:2022-01-1507:48:33【问题描述】:我有SpringWeb应用程序(JPA/Hibernate+MySQL)。我有三门课。Top10publicclassTop10@IdProductid;Productname;Integersoluong;... 查看详情

Thymeleaf 无法解析多模块 Spring Boot 项目中的模板

】Thymeleaf无法解析多模块SpringBoot项目中的模板【英文标题】:Thymeleafcan\'tresolvetemplatesinmultimoduleSpringBootproject【发布时间】:2020-05-1422:05:00【问题描述】:我遇到了标题中描述的问题。我创建了具有以下结构的多模块SpringBoot项目... 查看详情

带有 Java/Spring Boot 的 GraphQL 无法从传递的查询中选择定义为在模式中的字段上定义的自定义指令

】带有Java/SpringBoot的GraphQL无法从传递的查询中选择定义为在模式中的字段上定义的自定义指令【英文标题】:GraphQLwithJava/SpringbootunabletopickcustomdirectivedefinedasonFieldinschemafrompassedquery【发布时间】:2021-08-1120:42:52【问题描述】:已... 查看详情

无法在 Spring Boot 应用程序中读取 application.properties

】无法在SpringBoot应用程序中读取application.properties【英文标题】:Unabletoreadapplication.propertiesinspringbootapplication【发布时间】:2018-02-1902:01:38【问题描述】:我无法在作为bean从另一个项目注入的类中读取项目中的application.properties... 查看详情

无法从 Spring Boot MongoDB 中的数组中删除嵌入对象

】无法从SpringBootMongoDB中的数组中删除嵌入对象【英文标题】:UnabletoremoveembeddedobjectfromarrayinSpringBootMongoDB【发布时间】:2022-01-0612:09:43【问题描述】:我有一个集合“用户”,其中包含字段“carts”,它是对象数组。像这样的_id... 查看详情

无法从 JUnit 中的 Spring Boot 读取应用程序属性?

】无法从JUnit中的SpringBoot读取应用程序属性?【英文标题】:NotabletoreadapplicationpropertiesfromSpringBootinJUnit?【发布时间】:2017-06-2613:16:32【问题描述】:我有一个JUnit测试,我想针对REST服务运行该服务,该服务已经在我的机器上的... 查看详情

无法从 Spring Boot REST 中的 Hibernate POJO 返回 JSON

】无法从SpringBootREST中的HibernatePOJO返回JSON【英文标题】:UnabletoreturnJSONfromHibernatePOJOinSpringBootREST【发布时间】:2018-07-0716:53:41【问题描述】:我正在尝试使用SpringBoot创建一个基本的REST服务,该服务返回使用Hibernate从数据库创建... 查看详情

SpringDoc openAPI 工具未扫描 Spring Boot 项目中的 API

...gBootProject【发布时间】:2020-05-1206:44:44【问题描述】:我无法让它工作。我从一个工作正常的SpringBoot(v2.2.2)应用程序和一个工作的@RestController开始。为了让springdoc工作,我在我的pom 查看详情

无法从 Spring Boot 应用程序中的自定义 yml 文件加载配置

】无法从SpringBoot应用程序中的自定义yml文件加载配置【英文标题】:Cannotloadconfigfromcustomymlfileinspringbootapplication【发布时间】:2017-11-1608:30:55【问题描述】:我正在从我的SpringBoot服务中的application.yml加载自定义配置。我已经通... 查看详情

如何从 Spring Boot 项目中的子子表中检索数据?

】如何从SpringBoot项目中的子子表中检索数据?【英文标题】:HowdoIretrivedatafromsubchieldtableinmySpringBootProject?【发布时间】:2021-08-3117:00:15【问题描述】:我有两个实体“User”和“UserOtp”,它们具有一对多的关系。我想在一张表中... 查看详情

如何从 Spring Boot 中的资源中读取 JSON 文件

】如何从SpringBoot中的资源中读取JSON文件【英文标题】:HowtoreadJSONfilefromresourcesinSpringBoot【发布时间】:2020-03-0109:12:41【问题描述】:使用SpringBoot2.1.5Release,创建了以下示例SpringBoot微服务:Maven项目结构:MicroService│pom.xmlsrc│... 查看详情

Gradle Spring Boot 应用程序无法从 Json 文件中提取数据

】GradleSpringBoot应用程序无法从Json文件中提取数据【英文标题】:GradleSpringBootApplicationFailedtoExtractDatafromJsonfile【发布时间】:2022-01-2011:36:01【问题描述】:我创建了带有springboot依赖项的Gradle项目,这是一个restapi。我默认使用netb... 查看详情

由于 kotlin 错误,Java Spring Boot 应用程序无法从 IDEA 运行

】由于kotlin错误,JavaSpringBoot应用程序无法从IDEA运行【英文标题】:JavaSpringBootapplicationdoesn\'trunfromIDEAduetokotlinerror【发布时间】:2021-08-1511:31:00【问题描述】:我有一个纯Java编码的JavaSpringBoot应用程序。当我尝试使用运行配置从... 查看详情

是否可以从 Maven 多模块项目的父模块中的配置文件夹加载 Spring-Boot 属性?

...是否可以从Maven多模块项目的父模块中的配置文件夹加载Spring-Boot属性?【英文标题】:IsitpossibletoloadSpring-BootpropertiesfromconfigfolderwithinparentmoduleofaMavenmulti-moduleproject?【发布时间】:2021-05-0216:58:53【问题描述】:是否可以从多模块... 查看详情