I am working with spring boot an thymeleaf and try to get an image from the application.
<img th:src="@{/static/img/png-test.png}"/>
this is my tag in my template file. which gets rendered to
<img src="/static/img/png-test.png">
I already tried to add my own ResourceHandler
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
which seems to work because on startup I get the following message
2017-08-04 20:16:02.926 INFO 15234 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/static/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
but there is no image showing up inside the browser. instead I get the following error in the browser console
GET http://localhost:8080/static/img/png-test.png 405 ()
Spring logs the following
2017-08-04 19:59:20.265 WARN 14705 --- [nio-8080-exec-5] o.s.web.servlet.PageNotFound : Request method 'GET' not supported
EDIT:
Could it be a problem that i added this to my build.gradle
file
jar {
from('src/main/') {
include 'static/'
}
}
because otherwise gradle never put the static folder into the jar
EDIT: I found out what is needed to reproduce the failure
I have a function inside my controller which looks like this
@RequestMapping(name = "/", method = RequestMethod.POST)
public String func(@RequestParam(name = "var1") String var1, @RequestParam(name = "var2") String var2){
return "templatename";
}
When I uncomment the function I am able to receive static content. at least method not allowed makes sense now. because it is expecting a POST.
So how can I fix this without having to remap the function?
I just noticed that i was just stupid. i wrote
@RequestMapping(name = "/", method = RequestMethod.POST)
instead of
@RequestMapping(value = "/", method = RequestMethod.POST)
over a function in my controller. which mapped this function to /**
So my requests to any static content was routed to this function which only accepted the POST
method.
But still a big thnx to @Kirby