springboot 整合 swagger2
整合步骤
- pom 添加依赖
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>
- 创建 swagger 配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {private static ApiInfo DEFAULT = null;@Beanpublic Docket docket() {return new Docket(DocumentationType.SWAGGER_2);}
}
- 浏览器访问:http://localhost:8080/swagger-ui.html(注意端口号)
定制配置
修改页面内容
修改 SwaggerConfig.java
@Configuration
@EnableSwagger2
public class SwaggerConfig {private static ApiInfo DEFAULT = null;@Beanpublic Docket docket() {Docket docket = new Docket(DocumentationType.SWAGGER_2);ApiInfo apiInfo = new ApiInfoBuilder().contact(new Contact("Swagger - k8sclient","https://github.com/vazquez/k8sclient","vazquez@gmail.com")).title("Kubernetes Client API").description("This is a simple example of a Spring Boot RESTful service.").version("1.0").build();docket.apiInfo(apiInfo);return docket;}
}
浏览器访问:http://localhost:8080/swagger-ui.html
去掉 basic-error-controller
方法一:指定包显示
修改 SwaggerConfig
@Configuration
@EnableSwagger2
public class SwaggerConfig {private static ApiInfo DEFAULT = null;@Beanpublic Docket docket() {Docket docket = new Docket(DocumentationType.SWAGGER_2);ApiInfo apiInfo = new ApiInfoBuilder().title("Kubernetes Client API").description("This is a simple example of a Spring Boot RESTful service.").version("1.0").build();docket.apiInfo(apiInfo).select().apis(RequestHandlerSelectors.basePackage("com.vazquez.k8sclient.controller")).build();return docket;}
}
方法二:指定方法显示
修改 SwaggerConfig
@Configuration
@EnableSwagger2
public class SwaggerConfig {private static ApiInfo DEFAULT = null;@Beanpublic Docket docket() {Docket docket = new Docket(DocumentationType.SWAGGER_2);ApiInfo apiInfo = new ApiInfoBuilder().title("Kubernetes Client API").description("This is a simple example of a Spring Boot RESTful service.").version("1.0").build();docket.apiInfo(apiInfo).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();return docket;}
}
在 controller 类中方法,即接口方法上添加 @ApiOperation
注解(只有添加了 @ApiOperation
注解的方法才显示)
@ApiOperation(value = "获取集群下的所有事件", notes = "获取集群下的所有事件")@GetMappingpublic Response<ApiResponse<EventsV1EventList>> listNamespacedEvent(@PathVariable String clusterName, @PathVariable String namespaceName) {return eventService.listNamespacedEvent(clusterName, namespaceName);}