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
Add a comment  | 

2 Answers 2

Reset to default 0

Instead 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 17Stack Overflow