Search code examples
javaspring-bootswaggerswagger-2.0springfox

Spring Boot Validation information not displayed in Swagger UI


My classes are written in Spring Boot Java and I use Swagger 2 to generate their documentation. I am using spring-fox version 2.9.0.

To show the validation constraints (@min, @max, @pattern, etc) in Swagger UI, I added the following lines to my application.java, where showExtensions(true), but that didn't work. Desired result in Swagger UI

enter image description here

What should I change to get the wanted result?

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

Solution

  • Your UiConfiguration is fine. What you need to do is (to activate the Springfox Support for JSR-303 cf. Springfox Reference Documentation):

    • add the springfox-bean-validators dependency to your pom.xml:

      <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-bean-validators</artifactId>
          <version>2.9.2</version> <!-- or any version you like -->
      </dependency>
      
    • Import the configuration from the springfox-bean-validators module:

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

    Now you should be able to see the desired information at the top of annotated attributes in Swagger UI.