一、swagger使用
swagger在测试接口方面十分简便,还可以控制bean中字段的正则校验。
1.添加依赖
pom.xml
<!--添加swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger2-UI-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.注解配置文件
SwaggerConfig.java
package miiteen.swaggerConfig;
import io.swagger.annotations.Api;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig{
/**
* 创建API
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
// 详细定制
.apiInfo(apiInfo("1.0.0"))
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
// 指定当前包路径
.apis(RequestHandlerSelectors.basePackage("miiteen.customs"))
// 扫描所有
.paths(PathSelectors.any())
.build();
}
/**
* 添加摘要信息
*/
private ApiInfo apiInfo(String version) {
// 用ApiInfoBuilder进行定制
return new ApiInfoBuilder()
.title("海关接口文档")
.version(version)
.build();
}
}
*注
3.bean文件
4.Controller
5.MiiteenApplication.java
@EnableSwagger2
@SpringBootApplication
public class MiiteenApplication {
public static void main(String[] args) {
SpringApplication.run(MiiteenApplication.class, args);
}
}