Swagger初体验

Posted by AaronYang on July 1, 2020

Swagger教程

Swagger是一款Restful接口的文档在线自动生成+功能测试软件。Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化Restful风格的Web服务。

构建Swagger

  1. 引入依赖
<!-- swagger -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.9.2</version>
</dependency>

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.9.2</version>
</dependency>
  1. 开启Swagger
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
}
  1. 访问本地连接
http://localhost:8080/swagger-ui.html#/

配置Swagger

配置Bean实例

//配置Swagger的Bean实例
@Bean
public Docket docket(Environment environment){
    Profiles profiles = Profiles.of("dev");
    boolean b = environment.acceptsProfiles(profiles);
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiinfo())
        // 是否启动Swagger, 如果为False,则Swagger不能在浏览器中访问
        .enable(b)
        .select()
        // 配置要扫描接口的方式
        // basePackage:指定扫描的包
        // any():扫描全部
        // none():不扫描
        // withClassAnnotation: 扫描类上的注解,参数时一个注解的反射对象
        // withMethodAnnotation: 扫描方法上的注解
        .apis(RequestHandlerSelectors.basePackage("com.lyricyang.container.cosmos.controller"))
        // 过滤
        //.paths(PathSelectors.ant("com/lyricyang/**"))
        .build();
}

配置APIInfo

// API说明信息
private ApiInfo apiinfo() {
    // 作者信息
    Contact contact = new Contact("Aaron Yang", "https://lyricyang.github.io/", "983732985@qq.com");
    return new ApiInfo(
        "Cosmos的API文档",
        "Cosmos个人管理网站整合Swagger",
        "v1.0",
        "http://cosmos.free.idcfengye.com/",
        contact,
        "Apache 2.0",
        "http://www.apache.org/licenses/LICENSE-2.0",
        new ArrayList());
}

配置分组

// 配置分组
.groupName("Cosmos")

如果需要多个分组,则需配置多个Docket的Bean

Swagger注解

  • @Api()用于类; 表示标识这个类是swagger的资源
  • @ApiOperation()用于方法; 表示一个http请求的操作 @ApiParam()用于方法,参数,字段说明; 表示对参数的添加元数据(说明或是否必填等)
  • @ApiModel()用于类 表示对类进行说明,用于参数用实体类接收
  • @ApiModelProperty()用于方法,字段 表示对model属性的说明或者数据操作更改
  • @ApiIgnore()用于类,方法,方法参数 表示这个方法或者类被忽略
  • @ApiImplicitParam() 用于方法 表示单独的请求参数
  • @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
@RestController
public class HelloController {

    @GetMapping("/hello")
    @ApiOperation(value="hello",tags={"你好"},notes="描述信息")
    @ApiImplicitParam(name = "param", value = "参数名", dataType = "string", paramType = "query")
    public String hello(@ApiParam("参数名") String param){
        return "hello " + param;
    }

    @PostMapping("/billdetail")
    public BillRecordDetail billDetail(){
        return new BillRecordDetail();
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("账单详细信息类")
public class BillRecordDetail {

    /**
     * 主键ID
     */
    @ApiModelProperty("主键ID")
    private int id;

    /**
     * 交易日期
     */
    @ApiModelProperty(value="消费日期",name="date",example="2020-07-01")
    private Date date;

    /**
     * 一级类别
     */
    private String catalogFirst;

    /**
     * 二级类别
     */
    private String catalogSecond;

    /**
     * 金额
     */
    private double money;

    /**
     * 描述
     */
    private String description;
}

*Guava包冲突解决

  • 打开项目中的Pom文件
  • 排除冲突

    Conflicts(查看冲突)

    All Dependencies as List(列表形式查看所有依赖)

    All Dependencies as Tree(树形式查看所有依赖)

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>${swagger.version}</version>
	<exclusions>
		<exclusion>
			<artifactId>guava</artifactId>
			<groupId>com.google.guava</groupId>
		</exclusion>
	</exclusions>
</dependency>