Multiple requestmapping value with path variables

前端 未结 4 1886
北海茫月
北海茫月 2021-02-03 10:08
@RequestMapping(value = {\"/abcd\", \"/employees/{value}/{id}\"})
public String getEmployees(
      @PathVariable(value = \"value\") String val, 
      @PathVariable(val         


        
4条回答
  •  北恋
    北恋 (楼主)
    2021-02-03 10:50

    We can't have optional path variables, you can have two controller methods which can call the same service.

    First Method

    @RequestMapping("/abcd")
    public String getEmployees(@RequestParam(value="param", required=false)String value){}
    

    Second Method

    @RequestMapping("/employees/{value}/{id}")
    public String getEmployees(@PathVariable(value="value") String val, @PathVariable(value="id") String id, @RequestParam(value="param", required=false) String value){}
    

    For @RequestParam we can use,

    @RequestParam(value="somevalue",required=false)

    for optional params rather than a pathVariable

提交回复
热议问题