做后端的,接口文档的编写,十分使得人头疼,Swagger可以算是这个接口文档中的框架了,现在我们来看看SpringBoot中配置Swagger。
在SpringBoot中配置Swagger很简单,主要分两个步骤:
1.在pom文件加入Swagger依赖
<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>
2.写一个启动Swagger的类。
@Configuration @EnableSwagger2//注解加上 public class ClientRoleSwagger { /** * 创建API应用 * apiInfo() 增加API相关信息 * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现, * 本例采用指定扫描的包路径来定义指定要建立API的目录。 * @return */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com")) .paths(PathSelectors.any()) .build(); } /** * 创建该API的基本信息(这些基本信息会展现在文档页面中) * 访问地址:http://项目实际地址/swagger-ui.html * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("客户端的相关接口") .description("更多请关注http://www.baidu.com") .termsOfServiceUrl("http://www.baidu.com") .contact("***")//谁创建的 .version("1.0") .build(); } }之后在controller中加入 ApiOperation这个注解可以描述写的什么接口。