【问题标题】:Spring Boot Validation information not displayed in Swagger UISwagger UI 中未显示 Spring Boot 验证信息
【发布时间】:2019-11-13 14:20:55
【问题描述】:

我的课程是用 Spring Boot Java 编写的,我使用 Swagger 2 来生成他们的文档。 我正在使用 spring-fox 2.9.0 版

为了在 Swagger UI 中显示验证约束(@min、@max、@pattern 等),我在我的 application.java 中添加了以下几行,其中 showExtensions(true) ,但是没有用。 Desired result in Swagger UI

我应该改变什么以获得想要的结果?

@Bean
UiConfiguration uiConfig() {
  return UiConfigurationBuilder.builder() 
      .deepLinking(true)
      .displayOperationId(false)
      .defaultModelsExpandDepth(1)
      .defaultModelExpandDepth(1)
      .defaultModelRendering(ModelRendering.EXAMPLE)
      .displayRequestDuration(false)
      .docExpansion(DocExpansion.NONE)
      .filter(false)
      .maxDisplayedTags(null)
      .operationsSorter(OperationsSorter.ALPHA)
      .showExtensions(true)
      .tagsSorter(TagsSorter.ALPHA)
      .supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
      .validatorUrl(null)
      .build();
}

【问题讨论】:

    标签: java spring-boot swagger swagger-2.0 springfox


    【解决方案1】:

    您的UiConfiguration 很好。您需要做的是(激活 Springfox 对 JSR-303 的支持 cf.Springfox Reference Documentation):

    • springfox-bean-validators 依赖添加到您的pom.xml

      <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-bean-validators</artifactId>
          <version>2.9.2</version> <!-- or any version you like -->
      </dependency>
      
    • springfox-bean-validators模块导入配置:

      ...
      @Import({springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class})
      public class SwaggerDocumentationConfig { ... }
      

    现在您应该能够在 Swagger UI 中带注释的属性顶部看到所需的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-17
      • 2021-07-20
      • 2022-01-09
      • 2020-09-27
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多