java-Swagger2>记录SpringBoot MvcEndpoint
作者:互联网
我目前正在尝试在SpringBoot项目上试用Swagger2(效果很好),但是,它只能使用@RestController类.
我在想:
>可以用来拾取弹簧执行器MvcEndpoint吗?
>可以将Swagger2组件(例如/swagger-ui.html、/v2/api-docs)托管在management port (e.g. http://${management.address}:${management.port}
)下,而不是server.port上吗?
应用程序
@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(又名自定义执行器端点)
@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"
版本:
> SpringBoot:1.4.0.发布
>摇篮:3.0
解决方法:
是的,我们很乐意对其进行自定义,以选择“ spring-boot-starter-actuator”爆发端点.
关键是添加customerize RequestHandlerSelectors谓词,com.example.Swagger2Config.RequestHandlerSelectors是入门的一个很好的例子.
以下是配置类:
@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();
}
}
}
这是github中的demo项目.
2)我真的不知道您的“ address:port而不是server.port”的确切含义是什么,显然,它像“ localhost:8080”一样托管在“ address:port”中,请为此附加更多信息.
标签:swagger-2-0,spring-boot-actuator,spring-boot,java,swagger-ui 来源: https://codeday.me/bug/20191118/2026714.html