@RequestParam spring MVC,使两个请求参数之一成为必需

Nik*_*il 6 web-services controller spring-mvc request

我正在编写一个服务,其中我接受一个I​​D或一个位置,并且我想强制执行约束,即必须在我的@Controller中指定ID或位置

@Controller
public class HelloController {
    @RequestMapping(value="/loc.json",method = RequestMethod.GET)
    public @ResponseBody String localiaztionRequest(@RequestParam(value = "location",  required = false) String callback
            ,@RequestParam(value = "id",  required = false) String uuid
            ,@RequestParam(value = "callback",  required = false) String callback) {
        //model.addAttribute("message", "Hello world!");

        return "hello";
    }
Run Code Online (Sandbox Code Playgroud)

为了清楚起见,我希望每个请求都发送location参数或id参数。如何对一对输入参数施加这样的约束?另外,有人可以向我解释ModelMap的用法,model.addAttribute(“ message”,“ Hello World!”)有什么作用?抱歉,如果这些问题看起来太幼稚,那么我对Spring框架是陌生的。

提前致谢。

f.k*_*sis 6

我认为您应该将其分为两种不同的控制器方法

@RequestMapping(value="/loc.json",method = RequestMethod.GET, params={"location"})
public @ResponseBody String localizationRequestByLoc(@RequestParam String location, @RequestParam String callback) {
    //model.addAttribute("message", "Hello world!");

    return "hello";
}

@RequestMapping(value="/loc.json",method = RequestMethod.GET, params={"id"})
public @ResponseBody String localizationRequestById(@RequestParam String id, @RequestParam String callback) {
    //model.addAttribute("message", "Hello world!");

    return "hello";
}
Run Code Online (Sandbox Code Playgroud)