java - Spring Boot 验证信息未显示在 Swagger UI 中

标签 java spring-boot swagger swagger-2.0 springfox

我的类是用 Spring Boot Java 编写的,我使用 Swagger 2 生成它们的文档。 我正在使用 spring-fox 版本 2.9.0。

为了在 Swagger UI 中显示验证约束(@min、@max、@pattern 等),我将以下几行添加到我的 application.java 中,其中 showExtensions(true) ,但这没有用。 Swagger UI 中的预期结果

enter image description here

我应该改变什么才能得到想要的结果?

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

最佳答案

你的UiConfiguration没问题。您需要做的是(激活Springfox对JSR-303的支持参见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 中带注释的属性顶部看到所需的信息。

关于java - Spring Boot 验证信息未显示在 Swagger UI 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58839198/

相关文章:

java - 测试中使用 Quartz 时出现 NoSuchBeanDefinitionException

python - 移动 Flask-Restplus Swagger API 文档

java - 使用 GridBagLayout 需要帮助

java - 在 Java 代码中引用 XML 变量时发生 Android 错误

java - 在不使用 java.io.* 的情况下获取 FileChannel(使用纯 NIO)

java - 如何在POM文件中指定maven命令行选项?

java - 如何在 Android 的 ActionBar 上为应用程序图标添加点击监听器?

rest - 带有 Controller 的简单 Spring Boot 项目中的 "Circular view path"

node.js - 从 hapi-js 路由自动生成 swagger.yaml

json - 使用 Swagger ,如何定义持续时间参数?