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 ?
i dint use @EnableWebMVC. This worked for me and spring boot service server static content for default localhost:8888/ and also for localhost:8888/some/path/
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-static-content
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so you can modify that behavior by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method.
In a stand-alone web application the default servlet from the container is also enabled, and acts as a fallback, serving content from the root of the ServletContext if Spring decides not to handle it. Most of the time this will not happen (unless you modify the default MVC configuration) because Spring will always be able to handle requests through the DispatcherServlet.
By default, resources are mapped on /** but you can tune that via spring.mvc.static-path-pattern. For instance, relocating all resources to /resources/** can be achieved as follows:
You can also customize the static resource locations using spring.resources.static-locations (replacing the default values with a list of directory locations). If you do this the default welcome page detection will switch to your custom locations, so if there is an index.html in any of your locations on startup, it will be the home page of the application.
In addition to the ‘standard’ static resource locations above, a special case is made for Webjars content. Any resources with a path in /webjars/** will be served from jar files if they are packaged in the Webjars format.
Sure thing. There is a
spring.mvc.static-path-pattern
that you can use to override that: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.
I added
spring.resources.static-location=file:../frontend/build
inapplication.properties
index.html
is present in thebuild
folderUse can also add absolute path
spring.resources.static-location=file:/User/XYZ/Desktop/frontend/build