Accessing resources with spring

Asked

Viewed 33 times

2

Hello.

I am having some problems recovering an image in a folder of my project. Here is a basic explanation of what I am trying to do: In my application a form is submitted and next to it an image for each saved item.

For learning reasons, I save this image inside my database and when I need it, I search the database and write it to a folder inside the application, in my case 'Resources/uploads' and show it in HTML. My problem is that I am unable to recover this image in HTML when saved in any other folder if not 'Resources/Static'.

I’m doing the mapping, but I’m still not getting it. The image is saved by the back end in the folder, but when I try to show it I get error 404. When I leave it inside the Resources/Static I can access normally, but when updating the registration that includes the image, it is only updated in the view after restarting the server, although it appears in the folder in an updated form.

Anyway, the main question is, Am I doing the mapping right? If yes, how to find this image and show it inside the HTML in order to be able to view it in an updated way?.

I’m using spring and Thymeleaf.

HTML line with Thymeleaf in which I try to find the image:

<img th:src="@{'/uploads/'+${animal.idAnimal}}" class="card-img-top" alt="Foto do animal"/>

Mapping of the resource:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/uploads/**").addResourceLocations("/uploads/");
}

1 answer

0

Try this way:

registry.addResourceHandler("/resources/**")
                 .addResourceLocations("/resources/");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.