javaimagespring-bootvaadin-spring-boot

images not accessible by Vaadin Spring Application


In the example given here:

https://vaadin.com/docs/framework/application/application-resources.html

the images-folder is put inside the WEB-INF directory of the application.

In my Vaadin Spring application, I do not have a WEB-INF directory, so I put the images folder inside the "resources" folder instead. Here is what the folder structure inside of "src/main/resources" and inside of "target" looks like:

enter image description here

The problem is that my application cannot access the images there. I always get the same "File not found" exception.

I tried out different path descriptions, among others, the following:

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "images/pic.png"

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/classes/images/pic.png"

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/target/classes/images/pic.png"

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/applicationName/target/classes/images/pic.png"

... nothing worked !!

How can I make this images accessible to my Vaadin Spring application?


Solution

  • The example you linked to uses FileResource. It won't work with jar-packaged resources.

    Preferably, you can put your images in src/main/resources/VAADIN/themes/{theme}/ and use a ThemeResource :

    // Image as a file resource
    FileResource resource = new ThemeResource("images/image.png");
    

    Alternatively, put your resource to src/main/resources/ and then access it from classpath:

    getClass().getResourceAsStream("/images/image.png")