简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM