Java optional parameters

后端 未结 17 1732
遇见更好的自我
遇见更好的自我 2020-11-22 13:19

How do I use optional parameters in Java? What specification supports optional parameters?

17条回答
  •  礼貌的吻别
    2020-11-22 14:10

    If it's an API endpoint, an elegant way is to use "Spring" annotations:

    @GetMapping("/api/foos")
    @ResponseBody
    public String getFoos(@RequestParam(required = false, defaultValue = "hello") String id) { 
        return innerFunc(id);
    }
    

    Notice in this case that the innerFunc will require the variable, and since it's not api endpoint, can't use this Spring annotation to make it optional. Reference: https://www.baeldung.com/spring-request-param

提交回复
热议问题