Spring Boot 我可以用我自己的实现替换 ResourceHttpRequestHandler 吗?

Bar*_*rry 5 spring spring-mvc spring-boot

遇到这个问题,因为我有一个具有宁静服务的应用程序,但我也需要提供一些静态服务。在这个应用程序中,我也使用了EnableSwagger2注释。看起来这个类是公共的,我可以对它进行子类化,但我不确定如何配置它。我的目标是覆盖这条线,这样我就可以控制 404。

Bar*_*rry 5

所以我终于做到了这一点,它对我来说很成功。配置让我有点,但在这里。假设您ResourceHttpRequestHandler使用名为CustomResourceHandler. 在我Application.java这把它正确地连接起来:

@Bean
public ResourceHttpRequestHandler resourceHttpRequestHandler() {
    ResourceHttpRequestHandler requestHandler = new CustomResourceHandler();
    requestHandler.setLocations(Arrays.<Resource>asList(applicationContext.getResource("/")));
    return requestHandler;
}

@Bean
public SimpleUrlHandlerMapping sampleServletMapping(){
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setOrder(Integer.MAX_VALUE - 2);
    Properties urlProperties = new Properties();
    urlProperties.put("/**", "resourceHttpRequestHandler");
    mapping.setMappings(urlProperties);
    return mapping;
}

@Autowired
ApplicationContext applicationContext;
Run Code Online (Sandbox Code Playgroud)
  • 这个答案帮助我正确配置了映射器。
  • 这个关于上下文的答案帮助我在处理程序中正确设置了位置。我以不同的方式设置它们,但对于我的所有资源,它并没有 100% 正常工作