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:
Spring-Actuator
MvcEndpoint ? Swagger2
components (eg /swagger-ui.html
, /v2/api-docs
) be hosted under the management port (eg 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:
1.4.0.RELEASE
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.