g3b*_*blv 45 java spring spring-boot json-schema-validator
我正在使用Spring Boot和json-schema-validator.我正在尝试读取jsonschema.json从该resources文件夹调用的文件.我尝试了几种不同的方法,但我无法让它发挥作用.这是我的代码.
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("jsonschema.json").getFile());
JsonNode mySchema = JsonLoader.fromFile(file);
Run Code Online (Sandbox Code Playgroud)
这是文件的位置.
在这里,我可以在文件classes夹中看到该文件.
但是当我运行代码时,我得到以下错误.
jsonSchemaValidator error: java.io.FileNotFoundException: /home/user/Dev/Java/Java%20Programs/SystemRoutines/target/classes/jsonschema.json (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
我在代码中做错了什么?
Joh*_*ohn 62
花了很多时间试图解决这个问题后,终于找到了一个有效的解决方案.该解决方案使用了Spring的ResourceUtils.也适用于json文件.
感谢Lokesh Gupta精心编写的页面:博客
package utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.io.File;
public class Utils {
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class.getName());
public static Properties fetchProperties(){
Properties properties = new Properties();
try {
File file = ResourceUtils.getFile("classpath:application.properties");
InputStream in = new FileInputStream(file);
properties.load(in);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
return properties;
}
}
Run Code Online (Sandbox Code Playgroud)
Tah*_*lik 32
读取文件的最简单方法是:
Resource resource = new ClassPathResource("jsonSchema.json");
FileInputStream file = new FileInputStream(resource.getFile());
Run Code Online (Sandbox Code Playgroud)
Emm*_*osu 20
花了太多时间回到这个页面,所以就把这个留在这里:
File file = new ClassPathResource("data/data.json").getFile();
Run Code Online (Sandbox Code Playgroud)
pov*_*nko 19
非常简短的回答:您正在寻找特定类加载器范围内的属性而不是目标类.这应该工作:
File file = new File(getClass().getResource("jsonschema.json").getFile());
JsonNode mySchema = JsonLoader.fromFile(file);
Run Code Online (Sandbox Code Playgroud)
另外,看到这个:
PS如果项目已在一台机器上编译,之后已在另一台机器上启动或您在Docker中运行您的应用程序,则可能存在问题.在这种情况下,资源文件夹的路径可能无效.在这种情况下,最好在运行时确定资源的路径:
ClassPathResource res = new ClassPathResource("jsonschema.json");
File file = new File(res.getPath());
JsonNode mySchema = JsonLoader.fromFile(file);
Run Code Online (Sandbox Code Playgroud)
cac*_*co3 15
要可靠地从 Spring Boot 应用程序中的资源获取文件:
\nInputStream而URL不是Fileresourcespublic class SpringBootResourcesApplication {\n public static void main(String[] args) throws Exception {\n ClassPathResource resource = new ClassPathResource("/hello", SpringBootResourcesApplication.class);\n try (InputStream inputStream = resource.getInputStream()) {\n String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);\n System.out.println(string);\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\nClassPathResource是 Spring 的实现- 加载资源Resource的抽象方式。它是使用构造函数实例化的:ClassPathResource(String, Class<?>)
/hello是文件的路径\nClassLoader而不是Class,则可以省略斜杠Class加载资源\nClass而不是ClassLoader,因为与JPMS 中ClassLoader.getResource的不同Class.getResource项目结构:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mvnw\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mvnw.cmd\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pom.xml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 java\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 com\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 caco3\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 springbootresources\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 SpringBootResourcesApplication.java\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 resources\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 application.properties\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 hello\nRun Code Online (Sandbox Code Playgroud)\n上面的示例适用于 IDE 和 jar
\nFileInputStream和URLFile因为并不总是可以从类路径资源获取它\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mvnw\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mvnw.cmd\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pom.xml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 java\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 com\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 caco3\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 springbootresources\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 SpringBootResourcesApplication.java\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 resources\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 application.properties\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 hello\nRun Code Online (Sandbox Code Playgroud)\n但失败并显示:\njava.nio.file.NoSuchFileException: file:/home/caco3/IdeaProjects/spring-boot-resources/target/spring-boot-resources-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/hello\n at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)\n at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)\n at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)\nRun Code Online (Sandbox Code Playgroud)\n当 Spring Boot jar 运行时InputStream或URL\nJsonLoader.fromFile问题中的 可以替换为JsonLoader.fromURL方法:它接受URLSpring框架允许通过以下方式访问类路径资源ClassPathResource
你可以使用它:
\nresources@Value:\npublic class SpringBootResourcesApplication {\n public static void main(String[] args) throws Exception {\n ClassLoader classLoader = SpringBootResourcesApplication.class.getClassLoader();\n File file = new File(classLoader.getResource("hello").getFile());\n\n Files.readAllLines(file.toPath(), StandardCharsets.UTF_8)\n .forEach(System.out::println);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\nResourceLoader:\njava.nio.file.NoSuchFileException: file:/home/caco3/IdeaProjects/spring-boot-resources/target/spring-boot-resources-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/hello\n at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)\n at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)\n at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)\nRun Code Online (Sandbox Code Playgroud)\n小智 12
如果您在Resources文件夹下有例如config文件夹,那么我尝试过该类,希望可以很好地工作
File file = ResourceUtils.getFile("classpath:config/sample.txt")
//Read File Content
String content = new String(Files.readAllBytes(file.toPath()));
System.out.println(content);
Run Code Online (Sandbox Code Playgroud)
Bha*_*kar 11
下面是我的工作代码。
List<sampleObject> list = new ArrayList<>();
File file = new ClassPathResource("json/test.json").getFile();
ObjectMapper objectMapper = new ObjectMapper();
sampleObject = Arrays.asList(objectMapper.readValue(file, sampleObject[].class));
Run Code Online (Sandbox Code Playgroud)
希望对大家有所帮助!
小智 9
在此处查看我的答案:https : //stackoverflow.com/a/56854431/4453282
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
Run Code Online (Sandbox Code Playgroud)
使用这两个进口。
宣布
@Autowired
ResourceLoader resourceLoader;
Run Code Online (Sandbox Code Playgroud)
在某些功能中使用它
Resource resource=resourceLoader.getResource("classpath:preferences.json");
Run Code Online (Sandbox Code Playgroud)
在您的情况下,根据您的需要,您可以使用以下文件
File file = resource.getFile()
参考:http : //frugalisminds.com/spring/load-file-classpath-spring-boot/ 正如前面的答案中已经提到的,不要使用 ResourceUtils 它在部署 JAR 后不起作用,这也适用于 IDE部署后
陷入同样的问题,这对我有帮助
URL resource = getClass().getClassLoader().getResource("jsonschema.json");
JsonNode jsonNode = JsonLoader.fromURL(resource);
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案。可以帮助某人;
它返回 InputStream,但我假设您也可以从中读取。
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jsonschema.json");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
106478 次 |
| 最近记录: |