java Spring Controller 处理所有其他控制器不匹配的请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36594644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:38:42  来源:igfitidea点击:

Spring Controller to handle all requests not matched by other Controllers

javaspringmodel-view-controllercontrollermapping

提问by rurounisuikoden

I have a series of Controllers with Request Mappings that match certain URL's. I also want a Controller that will match any other URL not matched by the other Controllers. Is there a way to do this in Spring MVC? For example, could i have a controller with @RequestMapping(value="**") and change the order in which Spring Controllers are processed so this Controller is processed last to catch all unmatched requests? Or is there another way to achieve this behaviour?

我有一系列带有与特定 URL 匹配的请求映射的控制器。我还想要一个控制器,它将匹配其他控制器不匹配的任何其他 URL。有没有办法在 Spring MVC 中做到这一点?例如,我是否可以有一个带有 @RequestMapping(value="**") 的控制器并更改 Spring Controller 的处理顺序,以便最后处理该控制器以捕获所有不匹配的请求?还是有另一种方法来实现这种行为?

采纳答案by Rizwan M.Tuman

If your base url is like that= http://localhost/myapp/where myapp is your context then myapp/a.html, myapp/b.html myapp/c.html will get mapped to the first 3 method in the following controller. But anything else will reach the last method which matches **. Please note that , if you put ** mapped method at the top of your controller then all request will reach this method.

如果您的基本网址是这样的= http://localhost/myapp/其中 myapp 是您的上下文,那么 myapp/a.html, myapp/b.html myapp/c.html 将映射到以下控制器中的前 3 个方法. 但是其他任何事情都会到达与 ** 匹配的最后一个方法。请注意,如果您将 ** 映射方法放在控制器的顶部,则所有请求都将到达此方法。

Then this controller servrs your requirement:

然后这个控制器满足您的要求:

@Controller
@RequestMapping("/")
public class ImportController{

    @RequestMapping(value = "a.html", method = RequestMethod.GET)
    public ModelAndView getA(HttpServletRequest req) {
        ModelAndView mv;
        mv = new ModelAndView("a");
        return mv;
    }

    @RequestMapping(value = "b.html", method = RequestMethod.GET)
    public ModelAndView getB(HttpServletRequest req) {
        ModelAndView mv;
        mv = new ModelAndView("b");
        return mv;
    }

    @RequestMapping(value = "c.html", method = RequestMethod.GET)
    public ModelAndView getC(HttpServletRequest req) {
        ModelAndView mv;
        mv = new ModelAndView("c");
        return mv;
    }

@RequestMapping(value="**",method = RequestMethod.GET)
public String getAnythingelse(){
return "redirect:/404.html";
}

回答by patrykos91

@RequestMapping (value = "/**", method = {RequestMethod.GET, RequestMethod.POST})
public ResponseEntity<String> defaultPath() {
    LOGGER.info("Unmapped request handling!");
    return new ResponseEntity<String>("Unmapped request", HttpStatus.OK);
}

This will do the work with proper order of controller matching. It will be used when nothing is matched.

这将以正确的控制器匹配顺序完成工作。当没有任何匹配时,它将被使用。