更改 Spring Boot 中提供静态内容的默认 URL 映射 [英] Changing default URL mapping for Serving Static Content in Spring Boot

查看:36
本文介绍了更改 Spring Boot 中提供静态内容的默认 URL 映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中使用以下映射添加新控制器(非静态)后,我的静态资源立即停止工作

@RequestMapping(value = "/{postId}/{postUri:.+}", method = RequestMethod.GET)public String viewPost(@ModelAttribute("model") ModelMap 模型,PathVariable("postId") String postId, PathVariable("postUri") String postUri) {//做某事}

调试后发现我新添加的controller方法开始抓取静态资源,基本上优先于静态资源的默认映射.

例如,对以下静态资源的请求到达我的控制器而不是静态资源处理程序.

http://localhost:7999/css/bootstrap-2a31dca112f26923b51676cb764c58d5.css

我使用的是 Spring Boot 1.4

有没有办法修改提供默认静态内容的映射 URL,因为我不想修改我的控制器方法的 URL?

解决方案

当然可以.有一个 spring.mvc.static-path-pattern 可以用来覆盖它:

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

classpath:/static/css/foo.css 映射到 /resources/css/foo.css.

(我在 a862b6d 中更清楚地说明了这一点)>

话虽如此,我只能强烈建议您在那里更改路径.拥有一个捕获根上下文的路径变量确实是个坏主意.

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

@RequestMapping(value = "/{postId}/{postUri:.+}", method = RequestMethod.GET)
public String viewPost(@ModelAttribute("model") ModelMap model, PathVariable("postId") String postId, PathVariable("postUri") String postUri) {
          // do something
}

After debugging I found that my newly added controller method started picking up static resources, basically, it has taken precedence over the default mapping for static resources.

For example, Request to the below static resource reaches my controller instead of static resource handler.

http://localhost:7999/css/bootstrap-2a31dca112f26923b51676cb764c58d5.css

I am using spring boot 1.4

Is there a way to modify the mapping URL for serving default static content since I do not want to modify the URL of my Controller method ?

解决方案

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.

这篇关于更改 Spring Boot 中提供静态内容的默认 URL 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆