【问题标题】:Spring REST request mappingSpring REST 请求映射
【发布时间】:2023-03-09 17:46:01
【问题描述】:

我想配置 Spring 以将所有请求重定向到特定的控制器,而不考虑 URL(长度和参数)。我应该如何在 RequestMapping 注释中给出 URL 模式/正则表达式。我尝试使用下面的代码,但它不起作用。非常感谢这方面的任何帮助。

    @Controller
    @RequestMapping("/*")
    public class ServiceController {
        @RequestMapping( method = RequestMethod.GET, value = "*" )
        public void getProjectList(HttpServletRequest httpServletRequest,Model model){

        }
    }

【问题讨论】:

    标签: spring spring-mvc restful-url spring-restcontroller request-mapping


    【解决方案1】:

    你需要@RequestMapping(method = RequestMethod.GET, value = "/**")

    来自http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-patterns

    除了 URI 模板,@RequestMapping 注解也 支持 Ant 风格的路径模式(例如,/myPath/*.do)。

    来自http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html

    映射使用以下规则匹配 URL:

    • ? 匹配一个字符
    • * 匹配零个或多个字符
    • ** 匹配路径中的零个或多个目录

    【讨论】:

    • 我们之前使用过不带方法参数的上述控制器映射,用于维护服务器以显示服务器当前正在维护中。它有效...
    【解决方案2】:

    您是否尝试过使用正则表达式?

    类似:

    @RequestMapping("/{value:.}")
    

    【讨论】:

    • 您想在类的顶部或方法的顶部更改请求映射。我尝试了所有可能的方法,但它不起作用。
    猜你喜欢
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2019-11-25
    • 2012-07-02
    • 1970-01-01
    • 2013-04-24
    相关资源
    最近更新 更多