tyro
tyro

Reputation: 795

How to access a resource file in src/main/resources/ folder in Spring Boot

I'm trying to access xsd in src/main/resources/XYZ/view folder where XYZ/view folder are created by me and folder has abc.xsd which I need for xml validation.

When I try to access the xsd every time I get the result as null,

I have tried as,

1)

@Value(value = "classpath:XYZ/view/abc.xsd")
private static Resource dataStructureXSD;
InputStream is = dataStructureXSD.getInputStream();
Source schemaSource = new StreamSource(is);
Schema schema = factory.newSchema(schemaSource);

2)

Resource resource = new ClassPathResource("abc.xsd");
File file = resource.getFile();

and many more trails I made to get the resource or classloader etc.

Finally I get the xsd with,

File file = new File(new ClassPathResource("/src/main/resources/XYZ/view/abc.xsd").getPath()); Schema schema = factory.newSchema(file);

and it is working, I want to know why the other two trails would have gone wrong or why it didn't work for me and fine for others. :(

Or is there other good way of doing it which I'm missing

Upvotes: 11

Views: 81273

Answers (4)

Jan Bodnar
Jan Bodnar

Reputation: 11637

Both @Value and ResourceLoader work OK for me. I have a simple text file in src/main/resources/ and I was able to read it with both approaches.

Maybe the static keyword is the culprit?

package com.zetcode;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

@Component
public class MyRunner implements CommandLineRunner {

    @Value("classpath:thermopylae.txt")
    private Resource res;

    //@Autowired
    //private ResourceLoader resourceLoader;

    @Override
    public void run(String... args) throws Exception {
        
       // Resource fileResource = resourceLoader.getResource("classpath:thermopylae.txt");        

        List<String> lines = Files.readAllLines(Paths.get(res.getURI()),
                StandardCharsets.UTF_8);

        for (String line : lines) {
            
            System.out.println(line);

        }
    }
}

A full working code example is available in my Loading resouces in Spring Boot tutorial.

Upvotes: 4

Load files from Resouces,

@Autowired private ResourceLoader resourceLoader; ...

Resource fileResource = resourceLoader.getResource(config.getFilePath()); try (BufferedReader in = new BufferedReader(new FileReader(fileResource.getFile()))){ ... }

in applicaiton.yml

filePath: "classpath:StaticData/Test.csv"

Upvotes: 0

Enamul Haque
Enamul Haque

Reputation: 5053

You may use bellow like ..

  1. My resource file location is like bellow

enter image description here

I have use bellow like in spring boot

@Autowired
private ResourceLoader resourceLoader;

  try {     

   final Resource resource = resourceLoader.getResource("classpath:files/timezoneJson.json");
         Reader reader = new InputStreamReader(resource.getInputStream());
         String filedata =  FileCopyUtils.copyToString(reader);
    } catch (Exception e) {     
        e.printStackTrace();
    }

Upvotes: 1

Kevin Peters
Kevin Peters

Reputation: 3444

The @Value annotation is used to inject property values into variables, usually Strings or simple primitive values. You can find more info here.

If you want to load a resource file, use a ResourceLoader like:

@Autowired
private ResourceLoader resourceLoader;

...

final Resource fileResource = resourceLoader.getResource("classpath:XYZ/view/abc.xsd");

Then you can access the resource with:

fileResource.getInputStream() or fileResource.getFile()

Upvotes: 22

Related Questions