Search code examples
spring-bootweb-deployment

Changing default URL mapping for Serving Static Content in Spring Boot


My static resources stopped working as soon as I added a new Controller (non rest) in my application with the following mapping

@RequestMapping(value = "/{postId}/{postUri:.+}", method = RequestMethod.GET)
public String viewPost(@ModelAttribute("model") ModelMap model, PathVariable("postId") String postId, PathVariable("postUri") String postUri) {
          // do something
}

After debugging I found that my newly added controller method started picking up static resources, basically, it has taken precedence over the default mapping for static resources.

For example, Request to the below static resource reaches my controller instead of static resource handler.

http://localhost:7999/css/bootstrap-2a31dca112f26923b51676cb764c58d5.css

I am using spring boot 1.4

Is there a way to modify the mapping URL for serving default static content since I do not want to modify the URL of my Controller method ?


Solution

  • Sure thing. There is a spring.mvc.static-path-pattern that you can use to override that:

    spring.mvc.static-path-pattern=/resources/**
    

    will map classpath:/static/css/foo.css to /resources/css/foo.css.

    (I've made that clearer in a862b6d)

    Having said that, I could only strongly recommend to change your path there. Having a path variable that catches the root context is really a bad idea.