Read file from resources folder in Spring Boot

后端 未结 12 1084
盖世英雄少女心
盖世英雄少女心 2020-11-28 06:50

I\'m using Spring Boot and json-schema-validator. I\'m trying to read a file called jsonschema.json from the resources folder. I\'ve t

12条回答
  •  离开以前
    2020-11-28 07:35

    See my answer here: https://stackoverflow.com/a/56854431/4453282

    import org.springframework.core.io.Resource;
    import org.springframework.core.io.ResourceLoader;
    

    Use these 2 imports.

    Declare

    @Autowired
    ResourceLoader resourceLoader;
    

    Use this in some function

    Resource resource=resourceLoader.getResource("classpath:preferences.json");
    

    In your case, as you need the file you may use following

    File file = resource.getFile()

    Reference:http://frugalisminds.com/spring/load-file-classpath-spring-boot/ As already mentioned in previous answers don't use ResourceUtils it doesn't work after deployment of JAR, this will work in IDE as well as after deployment

提交回复
热议问题