开发的第一周

148 阅读1分钟

Swagger

前后端分离

Vue + SpringBoot

后端:

SpringBoot

控制层,服务层,数据访问层

前端

Vue

前端控制层,视图层

项目中使用Swagger

  • swagger2
  • ui

Springboot集成Swagger

1.导入依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency><dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>
​
// 特殊情况
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

2.编写配置类--配置Swagger


@Configuration
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 指定构建api文档的详细信息的方法:apiInfo()
                .apiInfo(apiInfo())
                .select()
                // 指定要生成api接口的包路径,这里把controller作为包路径,生成controller中的所有接口
                .apis(RequestHandlerSelectors.basePackage("com.mszl.blog.controller"))
                .paths(PathSelectors.any())
                .build();
    }
​
    /**
     * 构建api文档的详细信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 设置页面标题
                .title("Spring Boot搭建实际项目中开发的架构")
                // 设置接口描述
                .description("跟武哥一起学Spring Boot第18课")
                // 设置联系方式
                .contact("zs," + "微信公众号:程序员私房菜")
                // 设置版本
                .version("1.0")
                // 构建
                .build();
    }
}

常用API注解

// 实体类上的注解
@ApiModel
// 实体类字段上的注解
@ApiModelProperty// controller具体的post,get的方法上的注解
@ApiOperation
// 接口传入参数注解
@ApiParam