【问题标题】:Spring boot can I replace ResourceHttpRequestHandler with my own implementation?Spring boot 我可以用自己的实现替换 ResourceHttpRequestHandler 吗?
【发布时间】:2017-09-01 20:29:37
【问题描述】:

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

【问题讨论】:

    标签: spring spring-mvc spring-boot


    【解决方案1】:

    所以我终于做到了,它对我来说很成功。配置让我有点,但在这里。假设您将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;
    
    • Answer 帮助我正确配置了映射器。
    • Answer about context 帮助我在处理程序中正确设置了位置。我以不同的方式设置它们,但它并不能 100% 正常运行我的所有资源

    【讨论】:

    • 事实证明,如果您想作为 JAR 运行,则在设置位置以模仿 defaultresourcehandler 时需要进一步的映射。
    猜你喜欢
    • 2014-01-19
    • 2020-07-27
    • 2016-06-17
    • 2018-10-15
    • 2012-07-31
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多