Spring boot 下使用 Swagger

通过Swagger 可以更好的将后台的RESTfull API文档化,如下图所示:

Spring boot 下使用 Swagger

1. Swagger 主要依赖以下两个jar包:

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.0.2</version>
</dependency>

2. 建立一个与Spring Boot的启动类同级的类:Swagger .class,在启动项目后,就会自动扫描com.example.demo下的api,到此这就可以了。

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; /**
*  @Description:
*  @author miaoying
*  @date 2018/5/23
*/
@Configuration
@EnableSwagger2
public class Swagger { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("title test")
.description(" description test")
.termsOfServiceUrl("url test")
.contact("miaoying")
.version("1.0.0")
.build();
}
}

源码见:https://github.com/mistbor/swagger

上一篇:Shell 的变量功能


下一篇:iOS,文本输入,键盘相关