【问题标题】:Spring how to add resource handlers for static files from other originSpring如何为来自其他来源的静态文件添加资源处理程序
【发布时间】:2021-01-09 14:13:48
【问题描述】:

我正在尝试在 Spring + Angular 项目中实现对静态资源的处理请求。当 Spring 应用在 http://localhost:8080 上运行时,Angular 应用在​​ http://localhost:4200 上运行。

因此,当我需要获取图像时,Angular 会发送一个类似于 http://localhost:4200/screenShots/image-name.jpg 的请求。为了处理这类请求,我创建了WebConfig 类:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String path = "file:///" + *path to folder with images on my PC*
        registry.addResourceHandler("/screenShots/**")
                .addResourceLocations(path);
    }
}

它不起作用,我得到 404 (Not Found) 错误。我已经尝试过 addind 和删除 @EnableWebMvc@CrossOrigin 注释,尝试在我的类中添加 addCorsMappings() 方法,但没有任何帮助。

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/screenShots/**")
                .allowedOrigins("http://localhost:4200");
    }

但是,当我将 http://localhost:8080/screenShots/image-name.jpg (changed :4200 to :8080) 字符串放在 Google 地址栏中时,它会按预期返回我的图像,所以我想知道有没有办法添加某种@CrossOrigin 注释替代资源处理程序因为我还没有在 Internet 上找到任何解决方案。谢谢!

Github repo 适合那些好奇的人(只有 gameImagesRelease 分支包含当前代码):https://github.com/Vladyslav-Bahlai/OktenGames/tree/gameImagesRelease

【问题讨论】:

    标签: java spring spring-boot spring-mvc


    【解决方案1】:

    例如,我将显示我的MVC-configs。我有同样的问题。 Spring 没有看到 HTML 文件。我提请您注意 templates 文件夹。

    @Configuration
    public class MvcConfig implements WebMvcConfigurer {
    
        /**
         * Making our pages visible
         *
         * @param registry
         */
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/login").setViewName("login");
            registry.addViewController("/coffees").setViewName("coffees");
            registry.addViewController("/teas").setViewName("teas");
            registry.addViewController("/contacts").setViewName("contacts");
            registry.addViewController("/home").setViewName("home");
            registry.addViewController("/about").setViewName("about");
            registry.addViewController("/registration").setViewName("registration");
            registry.addViewController("/card").setViewName("card");
        }
    
    
        /**
         * This method helps Spring discover file paths
         *
         * @param registry
         */
        @Override
        public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
    
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
    
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/templates/");
        }
    }
    

    【讨论】:

    • 嗨,感谢您的快速回答,但您能否至少用几句话描述一下为什么我会收到 404 错误以及您的代码如何准确解决我的问题,因为我不明白 @ 是什么987654325@ 方法适用于我的情况,您在addResourceHandlers() 中添加了什么来帮助我?真的很棒!
    • 对不起,当我写这篇评论时,你上面的代码没有呈现出来
    • 我的回答对你有帮助吗?
    • 不是真的,我仍然不知道如何修改我的课程,因为在使用 html 模板时情况有所不同。我可能会尝试将我的图像存储在resources/static 文件夹中,但我怀疑它会有所帮助。我认为问题在于我的请求是从 localhost:4200 而不是 8080 发送的,因此资源处理程序会忽略它
    • 添加有问题的项目结构
    猜你喜欢
    • 2017-10-13
    • 1970-01-01
    • 2016-12-31
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2020-01-03
    • 2014-08-13
    相关资源
    最近更新 更多