Spring Request Mapping Mis

2019-08-01 19:33发布

问题:

I am using Spring mapping. And have the following mapping code. The problem is there are like 20 possible misspelling for these and others that need to be accounted for. Rather than add a RequestMapping for each url which would be like 30 or 40, is there a way to simply redirect these. I know the way I am doing is not clean and would appreciate advice on how to keep my request mappings to a minium. Thanks.

@RequestMapping("/protect")
public String protect(Model model) {
    QuickResponse qr = createQR();
    model.addAttribute("qr", qr);
    return "qr_general";
}

回答1:

I am unsure as to what can be misspelled, but I am thinking you are referring to the path that is being mapped to.

The @RequestMapping annotation's default value member takes String[], so you should be able to put all of your mappings in one location:

@RequestMapping({"/protect", "/protekt", "/proteckt", "/protext"})
public String protect(Model model) {
    QuickResponse qr = createQR();
    model.addAttribute("qr", qr);
    return "qr_general";
}