Swageer 协调前后端,可测试后端接口,最流行的API框架,RestFul 风格Api文档在线自动生成工具
swagger 简单使用
1、导入依赖
<!-- swagger依赖-->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
2.创建Hello工程
3、配置swagger2
@Configuration
@EnableSwagger2 //开启swagger
public class SwaggerConfiger {
}
运行测试,可通过http://localhost:8080/swagger-ui.html访问
SpringBoot无法访问swagger-ui.html(404)
因为资源路径映射问题,SpringBoot无法找到swagger-ui.html可以添加配置解决
配置swagger(config)
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 指定扫描的包路径来定义指定要建立API的目录。
* @return
*/
@Bean
public Docket coreApiConfig(Environment environment){
//设置要显示的swagger环境
Profiles profiles = Profiles.of("dev","test");
//通过environment.acceptsProfiles判断是否处在自己的设定环境当中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(adminApiInfo())
.enable(flag)
.groupName("adminApi")
.select()
//只显示admin下面的路径
// .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
//RequestHandlerSelectors:配置扫描接口的方式
//basePackage:指定要扫描的包
//any():扫描全部
//none():都不扫描
.apis(RequestHandlerSelectors.basePackage("com.behere.swagger.controller"))
.build();
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("dgs管理系统--api文档")
.description("dgs管理系统接口描述")
.version("1.0")
.contact(new Contact("dgs","http://baidu.com","18219653217@163.com"))
.build();
}
}
配置APi文档的分组
.groupName("看向未来213")
给Api文档添加注释的五个注解
@Api("用户实体类")
@ApiModel("用户实体类")
两个一样是标注实体类的注解
@ApiModelProperty("用户名") 给属性添加注解
@ApiOperation("方法注释") 给方法添加注解
@ApiParam("用户名") 给参数加注解
我的一整套配置:
依赖:
<!--swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
config配置:
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 指定扫描的包路径来定义指定要建立API的目录。
* @return
*/
@Bean
public Docket coreApiConfig(Environment environment){
//设置要显示的swagger环境
Profiles profiles = Profiles.of("dev","test");
//通过environment.acceptsProfiles判断是否处在自己的设定环境当中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(adminApiInfo())
.enable(flag)
.groupName("adminApi")
.select()
//只显示admin下面的路径
// .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
//RequestHandlerSelectors:配置扫描接口的方式
//basePackage:指定要扫描的包
//any():扫描全部
//none():都不扫描
.apis(RequestHandlerSelectors.basePackage("com.behere.swagger.controller"))
.build();
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("dgs管理系统--api文档")
.description("dgs管理系统接口描述")
.version("1.0")
.contact(new Contact("dgs","http://baidu.com","18219653217@163.com"))
.build();
}
}
实体:
@ApiModel("用户实体类")
public class OrgUser implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("用户名")
public String userName;
@ApiModelProperty("密码")
public String password;
}
接口:
@RestController
public class swaggerDemo {
@GetMapping(value = "/hello")
public String Hello(){
return "Hello";
}
@PostMapping(value = "/user")
public OrgUser user(){
return new OrgUser();
}
//@ApiOperation:注解来给API增加方法说明
@ApiOperation("Hello控制类")
@PostMapping(value = "/hello2")
public String hello2(@ApiParam("用户名") String userName){
return "hello"+userName;
}
}
启动:
@SpringBootApplication
@EnableSwagger2
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}