如何在Spring MVC中绑定2个GET方法并使用@RequestMapping进行区分?

时间:2015-02-10 10:09:24

标签: spring spring-mvc

我有两个不同的方法,两个都有相同的网址,但有不同的传入参数集,我可以使用@RequestMapping正确映射它们吗?

@RequestMapping(value = "/someurl", method = RequestMethod.GET)
public ModelAndView methodA (
    @RequestParam(value = "param1", required = false) String param1,
    @RequestParam(value = "param2", required = false) String param2) {

    return null;
}

@RequestMapping(value = "/someurl", method = RequestMethod.GET)
public ModelAndView methodB (
    @RequestParam(value = "array", required = false) String[] array) {

    return null;
}

1 个答案:

答案 0 :(得分:2)

您可以根据请求参数的存在或不存在来缩小映射请求,例如

@RequestMapping(value = "/someurl", method = RequestMethod.GET, params={"!param1", "!param2"})
public ModelAndView methodB (
        @RequestParam(value = "array", required = false) String[] array) {

    return null;
}
仅当请求中没有param1param2时,

才会映射到methodB,并且会向框架提供如何区分这两个映射的信息,因此您将无法获得任何信息。启动时的错误