简体   繁体   English

Spring-addResourceHandlers无法使用Rest Controller解决静态资源

[英]Spring - addResourceHandlers not resolving the static resources with Rest Controller

I have the following problem. 我有以下问题。 When configuring my Spring WebMVC application with ResourceHandlers and having a RestController class with a special @RequestMapping , the static resource will not be served, instead the RestController is being called. 当使用ResourceHandlers配置Spring WebMVC应用程序,并使用带有特殊@RequestMapping的RestController类时,将不会提供静态资源,而是会调用RestController。

Here is the code: 这是代码:

@Configuration
@ServletComponentScan
@EnableWebMvc
public class Main extends SpringBootServletInitializer implements ServletContextAware, WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(new Class[]{Main.class, Initializer.class, ContainerInitializer.class});
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new FacesServlet(), "*.jsf");
        servletRegistrationBean.setName("JSF Faces Servlet");
        servletRegistrationBean.setLoadOnStartup(1);
        return servletRegistrationBean;
    }

    @Bean
    public ServletRegistrationBean facesServletRegistratiton() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new FacesServlet(), new String[]{"*.xhtml"});
        servletRegistrationBean.setName("XHTML Faces Servlet");
        servletRegistrationBean.setLoadOnStartup(1);
        return servletRegistrationBean;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

       // Register resource handler for CSS and JS
       registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/")
            .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic())
            .resourceChain(true)
            .addResolver(new PathResourceResolver());

       // Register resource handler for images
       registry.addResourceHandler("/images/**").addResourceLocations("/WEB-INF/images/")
            .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic())
            .resourceChain(true)
            .addResolver(new PathResourceResolver());
    }
}

So I have two resource handlers for serving static resources like css, js files in the resources folder and an images folder for my static images. 因此,我在资源文件夹中有两个用于处理静态资源(如css,js文件)和用于静态图像的图像文件夹的资源处理程序。

The code for my RestController class looks as follows: 我的RestController类的代码如下所示:

@RestController
@Component
public class Clownfish {
   @GetMapping(path = "/{name}/**")
   public void universalGet(@PathVariable("name") String name, @Context HttpServletRequest request, @Context HttpServletResponse response) {
         String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
         if (name.compareToIgnoreCase(path) != 0) {
             name = path.substring(1);
         }
    }
}

When calling my resource with http://localhost:8080/images/foo.png the RestController is being called. 当使用http:// localhost:8080 / images / foo.png调用我的资源时,将调用RestController。 If I change @GetMapping(path = "/{name}/**") to @GetMapping(path = "/{name}") it works correctly. 如果我将@GetMapping(path = "/{name}/**")更改为@GetMapping(path = "/{name}")它可以正常工作。 But I need the wildcards to handle special calls. 但是我需要通配符来处理特殊呼叫。

What am I doing wrong in my configuration? 我的配置在做什么错? TIA TIA

I found a way to set the priority of the ResourceHandler. 我找到了一种设置ResourceHandler优先级的方法。

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

       // Register resource handler for CSS and JS
       registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/")
            .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic())
            .resourceChain(true)
            .addResolver(new PathResourceResolver());

       // Register resource handler for images
       registry.addResourceHandler("/images/**").addResourceLocations("/WEB-INF/images/")
            .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic())
            .resourceChain(true)
            .addResolver(new PathResourceResolver());
       registry.setOrder(-1);  // This will set the priority lower to the default handler (that is by default 0)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM