Changing default URL mapping for Serving Static Content in Spring Boot

后端 未结 5 1677
鱼传尺愫
鱼传尺愫 2021-01-19 13:29

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

@RequestMapping(value = \"/{p         


        
相关标签:
5条回答
  • 2021-01-19 14:05

    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:

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

    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.

    0 讨论(0)
  • 2021-01-19 14:07

    For no controller pages:

    @Controller
    @RequestMapping("/feature")
    public class DataTableController {
    
        // map /feature/* to /feature/*
        @RequestMapping(value="/{name}", method = RequestMethod.GET)
        public ModelAndView staticPage(@PathVariable String name){
            return new ModelAndView("feature/" + name);
        }
    
    }
    

    For static resource except for HTML:

    @EnableWebMvc
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        // map /res/ to classpath:/resources/static/
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/res/**").addResourceLocations("classpath:/static/");
        }
    }
    
    0 讨论(0)
  • 2021-01-19 14:14

    I added spring.resources.static-location=file:../frontend/build in application.properties

    index.html is present in the build folder

    Use can also add absolute path

    spring.resources.static-location=file:/User/XYZ/Desktop/frontend/build

    0 讨论(0)
  • 2021-01-19 14:15

    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.

    0 讨论(0)
  • 2021-01-19 14:29

    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/

    @Configuration
    public static class WebServerStaticResourceConfiguration extends WebMvcConfigurerAdapter {
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/some/path/").setViewName("forward:/index.html");
        }
    }
    
    0 讨论(0)
提交回复
热议问题