Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger2 > Document a SpringBoot MvcEndpoint

I'm currently in the process of trying out Swagger2 on my SpringBoot project (it works great), however, it only picks up my @RestController classes.

I was wondering:

  1. Can it be used to pick up a Spring-Actuator MvcEndpoint?
  2. Can the Swagger2 components (e.g. /swagger-ui.html, /v2/api-docs) be hosted under the management port (e.g. http://${management.address}:${management.port}), instead of server.port?

Application.java

@EnableSwagger2
@SpringBootApplication
public class Application {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

AdminController.java (aka custom Actuator endpoint)

@Component
public class AdminController implements MvcEndpoint { ... }

application.yml

server.port: 8080
management.address: 127.0.0.1
management.port: 8081

build.gradle

compile("org.springframework.boot:spring-boot-starter-actuator")
compile "io.springfox:springfox-swagger2:2.5.0"
compile "io.springfox:springfox-swagger-ui:2.5.0"

Versions:

  • SpringBoot: 1.4.0.RELEASE
  • Gradle: 3.0
like image 681
Nick Grealy Avatar asked Jun 24 '26 16:06

Nick Grealy


1 Answers

Yes, it is eaiser to customize it to pick the "spring-boot-starter-actuator" exprosed endpoints.

The key point is add the customerize RequestHandlerSelectors predicate, the com.example.Swagger2Config.RequestHandlerSelectors is a good example to starter.

Following is the configuration class:

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket actuator() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Spring Boot Actuator")
                .select()
                .apis(RequestHandlerSelectorsExt.withInterface())
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("App")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    static class RequestHandlerSelectorsExt {
        public static Predicate<RequestHandler> withInterface() {
            return new Predicate<RequestHandler>() {
                @Override
                public boolean apply(RequestHandler input) {
                    return declaringClass(input) == EndpointMvcAdapter.class;
                }
            };
        }

        private static Class<?> declaringClass(RequestHandler input) {
            return input.getHandlerMethod().getMethod().getDeclaringClass();
        }
    }
}

Then you can get the API in the swagger ui. enter image description here

Here is the demo project in github.

2) I really don't know what your exact meaning of "address:port, instead of server.port", obviously it's hosted in "address:port" just like "localhost:8080", please append more info for this.

like image 153
Liping Huang Avatar answered Jun 26 '26 06:06

Liping Huang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!