【问题标题】:Springfox Java Bean Validations not displaying in Swagger OutputSpringfox Java Bean 验证未在 Swagger 输出中显示
【发布时间】:2017-11-15 16:28:04
【问题描述】:

我正在尝试遵循 Springfox Swagger 的文档以使 Java Bean Validation 正常工作 (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 UI 中捕获 NotNull 和 Size 注释,但事实并非如此。请帮助我了解这应该如何工作。谢谢。

.

感谢https://stackoverflow.com/users/8012379/indra-basak,我确实看到他们正在工作。但是,我必须将鼠标悬停在字段上才能像@Size 一样思考。请看下面的截图。

【问题讨论】:

    标签: spring-boot swagger springfox


    【解决方案1】:
    • 如果您使用的是 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"
    }
    

    【讨论】:

    • 我从您的帖子中看到它以前有效。但是我注意到我必须将鼠标悬停在该字段上才能查看@Size 信息之类的内容。不过,您的屏幕截图显示方式有所不同。我更新了我的原始问题以显示此截图。有什么想法吗?
    • 我的截图是由于io.springfox:springfox-swagger-ui:2.6.1库。虽然我使用的是最新的2.7.0 版本,但我的浏览器会从2.6.1 版本中获取一些较旧的静态文件,因为我没有清理浏览器缓存。
    • 啊,现在说得通了。似乎 2.7.0 仅在您将鼠标悬停时才显示信息。再次感谢。
    猜你喜欢
    • 2018-07-28
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多