简体   繁体   English

无法在 spring 启动时从配置中读取文件

[英]Cannot read file from config in spring boot

I am working on spring boot app and in my config file I have couple o file and I want to read them.我正在开发 spring 启动应用程序,在我的配置文件中有几个 o 文件,我想阅读它们。 so I have tried below stuffs.所以我尝试了下面的东西。

-Dspring.profiles.active=local -Dserver.port=8080 -Dajp.port=0 -Dspring.datasource.url=jdbc:oracle:thin:@localhost:1521:OraDoc -Dspring.datasource.username=system -Dspring.datasource.password=MyPasswd123 -Dspring.config.additional-location=file:/Users/bharatsuthar/HGW/codebaseDevelopmentRepo1/data-generation-tool/rdk-factory-data-config/ -Dlogging.config=file:/Users/bharatsuthar/HGW/codebaseDevelopmentRepo1/data-generation-tool/rdk-factory-data-config/logback-spring.xml

I am able to read all files since spring is supporting this by using -Dspring.config.additional-location but I am not sure how to read test.pem file in my java code as a String only.我能够读取所有文件,因为 spring 通过使用-Dspring.config.additional-location支持此功能,但我不确定如何仅将 java 代码中的test.pem文件作为字符串读取。

below is my test.pem file location under rdk-factory-data-config directory下面是我在rdk-factory-data-config 目录下的 test.pem 文件位置

[location of test pem file] ( https://i.stack.imgur.com/4T0Y8.png ) [测试 pem 文件的位置] ( https://i.stack.imgur.com/4T0Y8.png )

I tried with -Dspring.config.additional-location here i am able to read file like application.yml and application-dev.yml but how can I read the test.pem file to String in my java code.我在这里尝试使用-Dspring.config.additional-location我能够读取 application.yml 和 application-dev.yml 之类的文件,但是如何在我的 java 代码中将 test.pem文件读取为字符串。 I want to read pem file into String so that I can read the data in my spring boot app.我想将 pem 文件读入字符串,以便我可以在我的 spring 启动应用程序中读取数据。

content of pem file: pem文件内容:

-----BEGIN CERTIFICATE-----
MIIChjCCAg2gAwIBAgIQKgFtzZclg1CxcBAHZDc2cTAKBggqhkjOPQQDAzAzMQsw
CQYDVQQGEwJTSzEUMBIGA1UEChMLUkRLIEZhY3RvcnkxDjAMBgNVBAMTBVNLIFIx
MB4XDTIyMTAxMzA5MDAzMloXDTQ3MTAxMzIzNTk1OVowMzELMAkGA1UEBhMCU0sx
FDASBgNVBAoTC1JESyBGYWN0b3J5MQ4wDAYDVQQDEwVTSyBDMTB2MBAGByqGSM49
AgEGBSuBBAAiA2IABDSRscO50mm3uzavmLTS2DEw/J/1dRp5nmyWJNMp+B0/w5y7
AAvOLtcJDpulCFCXa/UhEfvbGEVvDtS9jY75YtOqvQkPIIuE6IUC6kyTXZaPOe+3
WwYbzYxpOXuMDOzZbaOB5TCB4jAOBgNVHQ8BAf8EBAMCAQYwHQYDVR0OBBYEFJJS
jPquJiIZ49geOU/NCAsvRaPOMB8GA1UdIwQYMBaAFPS8o9tW37TZ6W+OKy7hznHt
FR0dMBIGA1UdEwEB/wQIMAYBAf8CAQAwNwYDVR0fBDAwLjAsoCqgKIYmaHR0cDov
L2NybC5yZGsudGVsZXNlasdfasdfsYy5kZS9ybC9TS19SMS5jcmwwQwYIKwYBBQUHAQEENzA1
MDMGCCsGAQUFBzAqwrrewrChidodHRwOi8vY3J0LnJkay50ZWxlc2VjLmRlL2NydC9TS19S
MS5jcnQwCgYIKoZIzj0EAwMDZwAwZAIwITubLwiMJ6KpTmyI41kOcCd3VzjbdPsx
NB3RZIBf4o3BYBzT1FQqbXvnYHsEGX/oAjAm8cTa9+tcLrrxN/RGwr53apg6FYlz
xRKbLjARTg/dFAn4W0w59XZXJ/fBZ4F6m5A=
-----END CERTIFICATE-----

I have tried this -我试过这个 -

String s = new String(Files.readAllBytes(Paths.get("test.pem")));           System.out.println("s=" + s);

-> it gives me no such file exception --> -> 它没有给我这样的文件异常 -->

Caused by: java.nio.file.NoSuchFileException: test.pem

---> I don't want to give full absolute path since my requirement is to read from directory which is in the root of my spring boot folder ---> 我不想给出完整的绝对路径,因为我的要求是从我的 spring 引导文件夹的根目录中读取

thank you so much太感谢了

As far as I understand, -Dspring.config.additional-location reads any property file or equivalent of it.据我了解, -Dspring.config.additional-location读取任何属性文件或等效文件。 *.pem files are not equivalent of property files so they should be ignored. *.pem 文件不等同于属性文件,因此应忽略它们。 Read Spring documentation 阅读 Spring 文档

What you should do instead, put a property in your property file specifying your.pem file location and later, use that property to read the contents of your.pem file in whatever format seems fit to you.您应该做的是,在您的属性文件中放置一个属性,指定您的 .pem 文件位置,然后使用该属性以适合您的任何格式读取您的 .pem 文件的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 spring 引导 2.4 中的配置服务器读取属性 - Read property from config server in spring boot 2.4 在 Spring Boot 中从 resources 文件夹中读取文件 - Read file from resources folder in Spring Boot 无法从 spring boot .jar 读取文件 - Unable to read file from spring boot .jar Spring boot fat jar 无法从 application.properties 中定义的绝对路径读取文件 - Spring boot fat jar cannot read file from absolute path defined in application.properties Spring Boot从配置文件加载列表返回空列表 - Spring Boot loading a list from a config file returns empty list Spring 启动微服务 - 配置服务器无法在 Docker 中获取属性文件(添加属性源:配置资源) - Spring Boot Microservices - Config Server cannot fetch property file (Adding property source: Config resource) in Docker 无法从Spring Boot中的/ static文件夹中读取资源 - Cannot read Resource from /static folder in Spring Boot 无法将JavaTimeModule导入Spring Boot配置类 - Cannot import JavaTimeModule into Spring Boot config class spring 批处理使用 spring 启动:从配置或命令行读取 arguments 并在作业中使用它们 - spring batch using spring boot: Read arguments from config or command line and use them in job 无法从Spring MVC中的属性文件读取 - cannot read from properties file in spring mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM