Spring Boot MVC请求映射会覆盖静态资源 [英] Spring Boot MVC request mapping overrides static resources

查看:321
本文介绍了Spring Boot MVC请求映射会覆盖静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Spring Boot中使用rest控制器来处理所有这样的请求:"/{arg}",除了"/sitemap.xml".我该如何实现?

解决方案

您可以通过正则表达式在控制器级别指定请求映射,并使用以下方法排除某些资源(例如'excludeResourceA'和'excludeResourceB'):

 @RestController
@RequestMapping(value = "/{arg:(?!sitemap.xml|excludeResourceA|excludeResourceB).*$}")
public class YourRestController {
    // your implementation
}
 

当然,您还可以在方法级别使用相对于控制器路径匹配的正则表达式指定请求映射,并且可以根据需要将方法签名中带有@PathVariable("arg") String arg的参数传递给方法主体./p>

I want to have rest controller in Spring Boot to handle all requests like this: "/{arg}", EXCEPT "/sitemap.xml". How can I achieve that?

解决方案

You could specify your request mapping on the controller level via regex and exclude some resources (e.g. 'excludeResourceA' and 'excludeResourceB') with:

@RestController
@RequestMapping(value = "/{arg:(?!sitemap.xml|excludeResourceA|excludeResourceB).*$}")
public class YourRestController {
    // your implementation
}

Of course you can also specify the request mapping on the method level with the same regex relative to your controller path matching and you can pass the argument with @PathVariable("arg") String arg in your method signature to your method body if you need it.

这篇关于Spring Boot MVC请求映射会覆盖静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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