Swagger2 > Document a SpringBoot MvcEndpoint

人盡茶涼 提交于 2019-12-05 20:33:24

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.

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