Spring Boot集成Swagger2

160 阅读2分钟

Swagger2

maven依赖

<!-- swagger2 配置 -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.4.0</version>
</dependency>
<!-- swagger2 原生UI -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.4.0</version>
</dependency>
<!-- swagger2 个性化UI -->
<dependency>
	<groupId>com.github.xiaoymin</groupId>
	<artifactId>swagger-bootstrap-ui</artifactId>
	<version>1.6</version>
</dependency>

Bean配置

package com.imooc.config;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

//    http://ip:port/swagger-ui.html     原生UI路径
//    http://ip:port/doc.html     个性化UI路径 需要添加swagger-bootstrap-ui依赖

    // 配置swagger2核心配置 docket
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)  // 指定api类型为swagger2
                    .apiInfo(apiInfo())                 // 用于定义api文档汇总信息
                    .select()
                    .apis(RequestHandlerSelectors
                            .basePackage("com.xxxx.controller"))   // 指定controller包
                    .paths(PathSelectors.any())         // 所有controller
                    .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxxxxxxx接口api")        // 文档页标题
                .contact(new Contact("imooc",
                        "https://www.xxx.com",
                        "abc@imooc.com"))        // 联系人信息
                .description("前后端交互API文档")  // 详细信息
                .version("1.0.1")   // 文档版本号
                .termsOfServiceUrl("https://www.xxx.com") // 网站地址
                .build();
    }

}

使用方式

  • @Api():用于Controller类 表示标识这个类是swagger的资源
@Api(value = "地址相关", tags = {"地址相关的api接口"})
@Controller
public class AddressController(){

}
  • @ApiOperation():用于Controller的方法上 表示一个http请求的操作
@Api(value = "地址相关", tags = {"地址相关的api接口"})
@Controller
public class AddressController(){
    @ApiOperation(value = "根据用户id查询收货地址列表", notes = "根据用户id查询收货地址列表", httpMethod = "POST")
    public void list(String userId){
    
        
    }
}
  • @ApiParam():用于方法,参数,字段说明 表示对参数的添加元数据(说明或是否必填等)
@Api(value = "地址相关", tags = {"地址相关的api接口"})
@Controller
public class AddressController(){
    @ApiOperation(value = "根据用户id查询收货地址列表", notes = "根据用户id查询收货地址列表", httpMethod = "POST")
    public void list(@ApiParam(name = "id", value = "id入参", required = true)String userId){
    

    }
}
  • @ApiModel():用于返回对象的类上 表示对类进行说明,用于参数用实体类接收
package com.imooc.pojo.bo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value = "用户对象BO", description = "从客户端,由用户传入的数据封装在此entity中")
public class UserBO {

}
  • @ApiModelProperty()用于方法,字段 表示对model属性的说明或者数据操作更改
package com.imooc.pojo.bo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value = "用户对象BO", description = "从客户端,由用户传入的数据封装在此entity中")
public class UserBO {

    @ApiModelProperty(value = "用户名", name = "username", example = "imooc", required = true)
    private String username;
    @ApiModelProperty(value = "密码", name = "password", example = "123456", required = true)
    private String password;
    @ApiModelProperty(value = "确认密码", name = "confirmPassword", example = "123456", required = false)
    private String confirmPassword;

}

  • @ApiIgnore():用于类,方法,方法参数 表示这个方法或者类被Swagger忽略
@ApiIgnore
@RestController
public class HelloController {

}
  • @ApiImplicitParam():用于方法 表示单独的请求参数
  • @ApiImplicitParams():用于方法,包含多个 @ApiImplicitParam 表示请求的参数组
  @Api("测试用例1")
  @Controller
  public class swaggerTestUse(){
      @ApiOperation(value = "apiOperationSwaggerTest", notes = "apiOperationSwagger测试")
      @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id入参", required = true, dataType = "Integer", paramType = "query"),
                        @ApiImplicitParam(name = "brand", value = "brand", required = true, dataType = "BRAND", paramType = "body")
    })
      public void apiOperationSwaggerTest(Integer id, Brand band){
      }
  }