Search code examples
restspring-bootswaggerswagger-uiswagger-2.0

required for input parameters on swagger


when I use @RequestParam with required = true on rest and while testing this on swagger, it will be shown along with *required mark next to it.

@GetMapping(path = "/getinfo")
    public ResponseEntity<?> getMyInfo(@RequestParam(value = "input", required = true)  int input, other request parameters)

But now how can I achieve same on swagger if i have mapped url with object using @ModelAttribute .

@GetMapping(path = "/getinfo")
        public ResponseEntity<?> getMyInfo(@ModelAttribute MyObject myObject)

Solution

  • You can try using the annotation @ApiParam

    @GetMapping(path = "/getinfo")
    public ResponseEntity<?> getMyInfo(@ModelAttribute("myObject") MyObject myObject)
    

    Inside your MyObject class

    public class MyObject {
    
      private long id;
    
      @ApiParam(name = "name", value = "Name is Mandatory", required = true)   
      private String name;
    
    }
    

    Now, name will be a *required field.