我使用的是Spring Boot 2.0.4,尝试读取放置在resources /目录中的json文件,但是得到了'FileNotFoundException'。
我从Stackoverflow和其他链接中引用了很多页面,但到目前为止没有成功。
下面是堆栈跟踪代码段,我不确定为什么会有'!'在类路径中:
/opt/springboot/myProject.jar!/BOOT-INF/classes!/schema/jsonValidationSchema.json at o.s.u.ResourceUtils.getFile(ResourceUtils.java:217) at o.s.u.ResourceUtils.getFile(ResourceUtils.java:180)
这是代码段:
InputStream in = new ClassPathResource("jsonValidationSchema.json").getInputStream();
JsonNode schemaJsonNode = JsonLoader.fromReader(new InputStreamReader(in, "UTF-8"));
JsonSchema schemaNode = ValidationUtils.getSchemaNode(schemaJsonNode);
参考文献:
File inside jar is not visible for spring
Read file from resources folder in Spring Boot
https://smarterco.de/java-load-file-from-classpath-in-spring-boot/
java.io.FileNotFoundException: class path resource cannot be opened because it does not exist