简体   繁体   中英

How can I use more than one @RequestParam annotations?

How do I use @RequestParam to bind one parameters of type String which is mandatory and another one which is of type Map<String, String> which is optional ?

    @RequestMapping(value = "users", method = RequestMethod.GET)
    public String getUsers(@RequestParam(name = "mandatory") String mandatory,
          @RequestParam(required = false) Map < String, String > optional) 
    throws Exception {
       return userService.getUsers(mandatory, optional);
   }

If what you want is to simply indicate that the "mandatory" parameter is required, you must add the required = true as follows:

@RequestMapping(value = "users", method = RequestMethod.GET)
    public String getUsers(@RequestParam(name = "mandatory", required = true) String mandatory,
          @RequestParam(required = false) Map < String, String > optional) 
    throws Exception {
       return userService.getUsers(mandatory, optional);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM