Swagger2 > Document a SpringBoot MvcEndpoint

送分小仙女□ 提交于 2019-12-07 18:10:04

问题


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

回答1:


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.

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.



来源:https://stackoverflow.com/questions/39159998/swagger2-document-a-springboot-mvcendpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!