swagger使用

 

一 swagger简介

Swagger 是一个用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。能够跟据业务代码自动生成相关的api接口文档html

具备如下好处java

• 及时性 (接口变动后,可以及时准确地通知相关开发人员)
• 规范性 (可以规范表达接口的地址,请求方式,参数及响应格式和错误信息)
• 一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)
• 可测性 (接口可以测试,以方便理解业务)

二 使用方法

1 引入相应jar包(信用分系统使用springboot 1.5.5.RELEAS版本与swagger2.6.1版本适合)spring

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

2建立 swagger 配置类(在 springboot 启动类 Application.java 同级建立 Swagger2 的配置类 Swagger2 )后端

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Value("${swagger.enable}")
    private boolean enableSwagger;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enableSwagger)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xx.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("信用分系统——api文档")
                .description("让咱们一块儿把信用分作好作强大")
                .termsOfServiceUrl("http://wiki.lianjia.com/pages/viewpage.action?pageId=xxxx")
                .version("1.0")
                .build();
    }
}

3 在 controller 和 vo 上使用特定含义注解api

@Api(description = "XX相关接口", tags = {"company"})
@RestController
public class XXController extends BaseController {
    @Resource
    private QueryCreditScoreCompService queryCreditScoreCompService;

    @ApiOperation(value = "查询分公司列表", notes = "查询分公司列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "loginUserCode", dataType = "string", required = true, value = "登录人系统号")
    })
    @ApiResponses({
            @ApiResponse(code = 999, message = "后端代码异常"),
            @ApiResponse(code = 404, message = "请求url路径错误,或者后端服务未部署启动"),
            @ApiResponse(code = 0, message = "正常返回")
    })
    @RequestMapping(value = "/api/xx/query", method = RequestMethod.GET)
    public JsonResult<List<CompDto>> queryCreditScoreCompList(String loginUserCode) {
        return new JsonResult<>();
    }
}

vo模型上添加返回字段含义springboot

@ApiModel("分公司信息")
@Data
public class CompDto {
    @ApiModelProperty("城市编码")
    private String cityCode;
    @ApiModelProperty("城市名称")
    private String compCode;
    @ApiModelProperty("分公司编码")
    private String cityName;
    @ApiModelProperty("分公司名称")
    private String compName;
}

访问部署机器的ip+端口+swagger-ui.html,就能看到接口api页面微信

三 各个注解用法说明:

注解 描述 解释
@Api Marks a class as a Swagger resource. 用于类对象,只有在类对象上标注该注解后,相应的api信息才能展现出来.
@ApiOperation Describes an operation or typically a HTTP method against a specific path. 描述具体的方法
@ApiImplicitParam Represents a single parameter in an API Operation. 描述具体的请求参数,好比具体的响应方法的参数为 HttpServletRequest时,我会从该参数中取一些请求参数,则能够经过该方法单独定义参数.见下面的具体说明,该参数不能单独使用,必须和@ApiImplicitParams一块儿使用,可参考后面的例子
@ApiImplicitParams A wrapper to allow a list of multiple ApiImplicitParam objects. 组合一组@ApiImplicitParam
@ApiParam Adds additional meta-data for operation parameters. 定义具体的请求参数,相似@RequestParam 参数,直接在方法参数上使用.
@ApiResponse Describes a possible response of an operation. 返回值,该类主要有code值与message两个属性,code值必须是http 返回code值,默认会有200,404等,不能单独使用,必须和@ApiResponses一块儿使用
@ApiResponses A wrapper to allow a list of multiple ApiResponse objects. 组合@ApiResponse
@ApiModel Provides additional information about Swagger models. 提供对请求参数与返回结果中的model的定义

四 注意的点 / 不足之处:

•  注意使用版本 springboot1.5.5 对应swagger2.6.1
•  tags标题由中文bug,中文的时候不能展开接口列表,须要用英文
•  注意:线上环境建议关闭
•  代码侵入性
•  若是系统控制内网访问,没法提供接口文档给其余公司人看