admin管理员组文章数量:1387353
I recently migrated my project to Java 17 and Spring Boot 2.5.7. After the migration, I'm encountering an issue where a JSON file located in my resources folder isn't being read correctly when running on Tomcat (WAR file deployment) in the testing environment. The same code works fine locally.
File Path:
pubs/src/main/resources/db.mongo.query/FORM16.json
Code:
String query = new String(
Files.readAllBytes(Paths.get(new ClassPathResource("db.mongo.query/" + this.name + ".json").getURI()))
);
Problem:
The file is located in the resources folder, but when the application is deployed on Tomcat (testing environment), I get an error when attempting to read the file. The code works correctly when running the application locally. I have confirmed that the Tomcat user has read permissions for the file. I have checked for a different file and it works as expected
Has anyone encountered this issue after migrating to Java 17 and Spring Boot 2.5.7? Any suggestions on how to resolve this?
I recently migrated my project to Java 17 and Spring Boot 2.5.7. After the migration, I'm encountering an issue where a JSON file located in my resources folder isn't being read correctly when running on Tomcat (WAR file deployment) in the testing environment. The same code works fine locally.
File Path:
pubs/src/main/resources/db.mongo.query/FORM16.json
Code:
String query = new String(
Files.readAllBytes(Paths.get(new ClassPathResource("db.mongo.query/" + this.name + ".json").getURI()))
);
Problem:
The file is located in the resources folder, but when the application is deployed on Tomcat (testing environment), I get an error when attempting to read the file. The code works correctly when running the application locally. I have confirmed that the Tomcat user has read permissions for the file. I have checked for a different file and it works as expected
Has anyone encountered this issue after migrating to Java 17 and Spring Boot 2.5.7? Any suggestions on how to resolve this?
Share Improve this question edited Mar 17 at 16:17 Komala Dunna asked Mar 17 at 16:13 Komala DunnaKomala Dunna 11 bronze badge 1- Are the files db.mongo.query/* in the build war file? Rename the file from .war to .zip and look inside. They should be at /WEB-INF/classes/db.mongo.query/*. If not then you have a build issue. Pease add your pom/gradle file and build command. – John Williams Commented Mar 18 at 15:43
2 Answers
Reset to default 0Instead of converting the class path resource to a file system path, open an input stream, and read from the input stream:
String query = StreamUtils.copyToString(
new ClassPathResource("db.mongo.query/" + this.name + ".json").getInputStream(),
StandardCharsets.UTF_8);
The below way is working
URL queryURL = getClass().getClassLoader().getResource("db.mongo.query/" + this.name + ".json");
String query = new String(Files.readAllBytes(Paths.get(queryURL.toURI())));
版权声明:本文标题:Issue with Reading JSON File from Resources Folder in Spring Boot after Migrating to Java 17 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744548034a2612014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论