Swagger2
- Swagger2是一个RESTful接口进行描述和展示的工具,可以通过 springfox-swagger2 整合,生成相应的文档,需引入两个库:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.5.0</version></dependency>
- 通过 @Configuration 注解为配置类,通过 @EnableSwagger2 启动 Swagger2,创建 Docket 的 Bean,通过 apiInfo() 方法创建api配置信息,select() 方法会返回一个ApiSelectorBuilder 实例用来控制哪些借口暴露给 Swagger 展现
@Configuration@EnableSwagger2public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.linyuan.paymentserver")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2构建RESTful APIs") .description("描述") .termsOfServiceUrl("http://localhost:8083") .contact("林塬") .version("1.0") .build(); }}
- 我们还可以在Controller上添加相应注解来为接口提供相应描述:
@Api:标记一个Controller类做为swagger
注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。