Swagger2>记录SpringBoot MvcEndpoint [英] Swagger2 > Document a SpringBoot MvcEndpoint

查看:624
本文介绍了Swagger2>记录SpringBoot MvcEndpoint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在我的 SpringBoot 项目中尝试 Swagger2 (这很棒),但是,它只能拿起我的 @RestController 类。



我想知道:


  1. 可以用来拿起 Spring-Actuator



    这是 demo 项目。



    2)我真的不知道你的确切含义是什么地址:端口,而不是server.port,显然它就像localhost:8080一样托管在address:port中,请为此附加更多信息。


    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

    解决方案

    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.

    这篇关于Swagger2&gt;记录SpringBoot MvcEndpoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆