Reputation: 2907
I want to serve static files from my SpringBoot application. I have this very simple controller that I wish does the stuff:
@EnableWebMvc
@RestController
public class MyRestController implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static")
.addResourceLocations("file:/static");
}
@PostMapping(path = "/hello")
public MyResponse hello(@RequestBody() MyBody body,
HttpServletRequest request) {
return new MyResponse("Hello " + request.getRemoteAddr());
}
}
My index.html file resides in the static folder:
MyApp/
src/
main/
static/index.html
static/img/image.png
When I do a GET request with curl to http://localhost:8080 the I get response code 404 in return and the server states No mapping for GET /
.
I expect that the index.html file is returned.
Sending a POST request to http://localhost:8080/hello
with a MyBody
object as a json body works though!
What have I done wrong?
I have read this blogpost from the Spring site, but it seems quiet old since that post was published in 2013. Maybe it works different today?
Upvotes: 0
Views: 4278
Reputation: 582
I have a blog application that receives uploaded images in a folder that can be outside static/.
MyApp/
src/
main/
resources/
static/
css/
javascript/
images/
blog/
1-blogpost/ (here are the uploaded images)
2-blogpost/ (here are the uploaded images)
(... and so on)
So i made this in Kotlin with Spring Boot:
@Configuration
class WebConfiguration : WebMvcConfigurer {
private val logger = loggerFactory.getLogger(WebConfiguration::class.java)
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
logger.info("####### Entering ResourceHandlers configurations #######")
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/")
registry.addResourceHandler("/blog/**").addResourceLocations("file:src/main/resources/blog/")
}
@Bean
fun restTemplate() : RestTemplate {
return RestTemplate()
}
}
Upvotes: 0
Reputation: 618
This is mentioned in the Spring Boot Documentation, under the spring mvc section you can use WebMvcConfigurer, but you do not need to do @EnableWebMvc
So you should remove the @EnableWebMvc annotation!
//@EnableWebMvc Remove this
@RestController
public class MyRestController implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static")
.addResourceLocations("file:/static");
}
@PostMapping(path = "/hello")
public MyResponse hello(@RequestBody() MyBody body,
HttpServletRequest request) {
return new MyResponse("Hello " + request.getRemoteAddr());
}
}
Upvotes: 3
Reputation: 691625
You should NOT use EnableWebMvc in Spring Boot. See the documentation
Upvotes: 1
Reputation: 2340
I think you are missing your resources folder, your folder structure should look like
MyApp/
src/
main/
resources/
static/index.html
static/img/image.png
Upvotes: 1
Reputation: 2283
Static resources usually go under /src/main/resources to get onto the classpath in the maven standard project layout, and Spring Boot should serve all files under /static (/src/main/resources/static) without any addResourceHandler()
application configuration.
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
Upvotes: 1