Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring中从@RequestMapping中排除url映射?_Java_Spring_Spring Mvc_Spring Security_Request - Fatal编程技术网

Java 如何在Spring中从@RequestMapping中排除url映射?

Java 如何在Spring中从@RequestMapping中排除url映射?,java,spring,spring-mvc,spring-security,request,Java,Spring,Spring Mvc,Spring Security,Request,我有一个请求映射,它处理上下文之后的任何字符串,例如www.example.com/anystring 我的处理方式如下: @RequestMapping(value="/{str}", method = RequestMethod.GET) public String getApp(@PathVariable("str") String anyString, ModelMap model) { //Do something } @RequestMapping("/ab

我有一个请求映射,它处理上下文之后的任何字符串,例如www.example.com/anystring

我的处理方式如下:

@RequestMapping(value="/{str}", method = RequestMethod.GET)
public String getApp(@PathVariable("str") String anyString, ModelMap model) {
        //Do something
    }
@RequestMapping("/about")
    public String getAboutPage() {
        return "about";
    }
问题是我的应用程序中有2-3个URL,URL如下:www.example.com/aboutwww.example.com/contact

我为它们编写了如下请求映射:

@RequestMapping(value="/{str}", method = RequestMethod.GET)
public String getApp(@PathVariable("str") String anyString, ModelMap model) {
        //Do something
    }
@RequestMapping("/about")
    public String getAboutPage() {
        return "about";
    }
但是很明显,因为我已经声明任何字符串都应该由
getApp()
处理,
getAboutPage()
永远不会执行。 如何从
getApp()
映射中排除
/about
/contact
等。 显然,我们可以在URL字符串中添加另一个关键字,但在我的应用程序用例中这是不可能的。 请帮忙(

编辑:

我是否应该处理
/about
/contact
内部
getApp()
之类的问题:

@RequestMapping(value="/{str}", method = RequestMethod.GET)
    public String getApp(@PathVariable("str") String anyString, ModelMap model) {

    if(anyString.equals("about")){
     //do about related stuff
    }

    if(anyString.equals("contact")){
     //do contact related stuff
    }

    //Do something
    }

是否有更好的方法?< /P> < P>在“catch ALL”映射中指定HTTP请求方法可能使路径匹配器认为它比绝对路径映射更具体。

在绝对路径上指定请求方法,映射比较器应该将绝对匹配排序在包含path变量的匹配之前

例如

或者,您可以删除catch all上的方法规范:

@RequestMapping("/{str}")

这完全取决于您的url结构以及这些路径是否接受不同的http请求方法。

Bang on!指定了该方法,现在就可以使用了!非常感谢@Sazzadur对其进行编辑以提高可读性。