Springfox Java Bean 验证未显示在 Swagger 输出中

Springfox Java Bean Validations not displaying in Swagger Output

我正在尝试按照 Springfox Swagger 的文档来使 Java Bean 验证工作(http://springfox.github.io/springfox/docs/current/#springfox-support-for-jsr-303),但它们没有出现在 Swagger UI.

这是我的Spring配置:

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();
    }
}

这是我的请求映射:

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

这是我的 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;
    }
}

我希望在 Swagger 中捕获 NotNull 和 Size 注释 UI 但它们不是。请帮助我理解这应该如何工作。谢谢。

.

感谢https://whosebug.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.

  • 如果您使用的是 springfox 版本 2.7.0@NotNull@Size 注释都应该有效。

  • 您的 @NotNull 注释已经在 password field.

  • 中工作
  • 如果字段存在 @ApiModelProperty 注释,则它优先于 @NotNull 注释。 username 字段就是这种情况。它显示为optional,因为@ApiModelProperty注解的required属性默认设置为false

如果你使用 springfox 版本 2.7.0 并且不使用 @ApiModelProperty 注释,模型将显示为:

验证

例如,如果您输入的用户名小于最小长度 4,则会出现以下异常:

{
  "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"
}