Search code examples
spring-bootswaggerspringfox

Springfox Java Bean Validations not displaying in Swagger Output


I'm trying to follow the documentation for Springfox Swagger to get Java Bean Validation to work (http://springfox.github.io/springfox/docs/current/#springfox-support-for-jsr-303), but they are not showing up in the Swagger UI.

This is my Spring Configuration:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Import({springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class})
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("My API")
                .build();
    }
}

This is my request mapping:

@ApiOperation(value = "Use to get token for internal applications")
@PostMapping(value = AuthUris.TOKEN)
public AuthResponse token(@Valid @RequestBody AuthRequest authRequest) {
// implementation omitted
}

This is my POJO:

@ApiModel
public class AuthRequest {
    @ApiModelProperty
    @NotNull
    @Size(min = 4, max = 50)
    private String username;
    @NotNull
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

I expected the NotNull and Size annotations to be captured in the Swagger UI but they are not. Please help me understand how this should work. Thank you.

enter image description here.

So thanks to https://stackoverflow.com/users/8012379/indra-basak I do see that they were working. However, I have to hover over the field to thinks like @Size. See the screenshot below.enter image description here


Solution

    • If you are using springfox version 2.7.0, both @NotNull and @Size annotations should work.

    • Your @NotNull annotation is already working in the password field.

    • If @ApiModelProperty annotation is present for a field, it takes precedence over @NotNull annotation. It is the case with the username field. It shows up as optional because the required attribute of @ApiModelProperty annotation is set to false by default.

    If you use springfox version 2.7.0 and don't use @ApiModelProperty annotation, the model will show up as:

    enter image description here

    Validation

    For example, if you enter a username less than the minimum size of 4, you will get the following exception:

    {
      "timestamp": 1511550365198,
      "status": 400,
      "error": "Bad Request",
      "exception": "org.springframework.web.bind.MethodArgumentNotValidException",
      "errors": [
        {
          "codes": [
            "Size.authRequest.username",
            "Size.username",
            "Size.java.lang.String",
            "Size"
          ],
          "arguments": [
            {
              "codes": [
                "authRequest.username",
                "username"
              ],
              "arguments": null,
              "defaultMessage": "username",
              "code": "username"
            },
            50,
            4
          ],
          "defaultMessage": "size must be between 4 and 50",
          "objectName": "authRequest",
          "field": "username",
          "rejectedValue": "s",
          "bindingFailure": false,
          "code": "Size"
        }
      ],
      "message": "Validation failed for object='authRequest'. Error count: 1",
      "path": "/tokens"
    }