Search code examples
jsonspringrestswaggerspringfox

How to make swagger-ui show certain fields in JSON as required (using springfox-swagger2 & ui)


I am using springfox-swagger2 and springfox-swagger-ui version 2.5.0 dependencies to generate my JSON APIs and also using swagger-ui to show the JSON messages.

Currently swagger ui shows my JSON model with all the fields as optional. How can I specify certain fields to be required. Do I need to annotate any field in my POJO to state that it is required. I am not using any swagger annotations and relying on springfox to generate JSON from my POJOs.

thanks


Solution

  • You must use the 'required' attribute of the @ApiModelProperty annotation

    public class LoginResponse {
    
        @ApiModelProperty(value="User's last name", required = true)
        String firstName;
    
        @ApiModelProperty(value="User's first name", required = true)
        String lastName;
    
        public LoginResponse(String firstName, String lastName) {
            this.firstName= firstName;
            this.deviceSecret = lastName;
        }
    
    }