Skip to content

Swagger UI

Yash edited this page Dec 17, 2021 · 1 revision

Integrating Swagger 2 and Swagger UI Into the Spring Boot RESTful Application

springfox doc, baeldung.com

The configuration of Swagger mainly centers around the Docket bean

After defining the Docket bean, its select() method returns an instance of ApiSelectorBuilder, which provides a way to control the endpoints exposed by Swagger.

We can configure predicates for selecting RequestHandlers with the help of RequestHandlerSelectors and PathSelectors. Using any() for both will make documentation for our entire API available through Swagger.

Swagger UI is a built-in solution that makes user interaction with the Swagger-generated API documentation much easier.

Filtering API for Swagger’s Response

It is not always desirable to expose the documentation for the entire API. We can restrict Swagger’s response by passing parameters to the apis() and paths() methods of the Docket class. PathSelectors provides additional filtering with predicates, which scan the request paths of our application. We can use any(), none(), regex(), or ant().

<!-- exclude slf4j API in both the jars -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
    <scope>compile</scope>
</dependency>
@Configuration
@EnableSwagger2
public class SpringFoxConfig {  

@Bean
public Docket apiProduct() {
    return new Docket(DocumentationType.SWAGGER_2).groupName("Application Services")
      .select()
      //.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
      .apis( RequestHandlerSelectors.withClassAnnotation(RestController.class) ) //Selection by RequestHandler - Only @RestController
      .paths(PathSelectors.any()) // .paths(regex("/product.*"))
      .build()
      .useDefaultResponseMessages(false)
      .apiInfo(apiInfo());
}

@Bean
public Docket apiActuator() {
    return new Docket(DocumentationType.SWAGGER_2).groupName("Actuator Services")
      .select()
      //Ignores controllers annotated with @RestController
      .apis( Predicates.not( RequestHandlerSelectors.withClassAnnotation(RestController.class) ) )//Selection by RequestHandler
      
      .paths(PathSelectors.ant("/actuatorPath/**")) // applicaiton.properties :: management.endpoints.web.base-path=/actuatorPath
      .build()
      .useDefaultResponseMessages(false)
      .apiInfo(metaData());
}

private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title("Spring Boot REST API")
            .description("\"Some custom description of API.\"")
            .version("1.0.0")
            .license("Apache License Version 2.0")
            .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
            .contact( new Contact("Yashwanth", "www.example.com", "myeaddress@company.com") )
            .build();
}
private ApiInfo metaData() {
    return new ApiInfoBuilder()
            .title("Actuator API")
            .description("\"Some custom description of API.\"")
            .version("1.0.0")
            .license("Apache License Version 2.0")
            .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
            .contact( new Contact("Yashwanth", "www.example.com", "myeaddress@company.com") )
            .build();
}

//Here is an example where we select any api that matches one of these paths
  private Predicate<String> paths() { // .paths(paths()) // and by paths
    return or(
        regex("/business.*"),
        regex("/some.*"),
        regex("/contacts.*"),
        regex("/springsRestController.*"),
        regex("/test.*"));
  }
}

Clone this wiki locally