美文网首页
Swagger的使用

Swagger的使用

作者: 面具猴 | 来源:发表于2019-05-24 16:32 被阅读0次

SpringBoot中使用Swagger2
1.依赖

        <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.添加配置类

@Configuration
@EnableSwagger2
public class Swagger2Config {

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("230电力测试模拟工具Api")
                .description("详细描述")
                .termsOfServiceUrl("协议三部工具软件组")
                .contact("fda")
                .version("1.0")
                .build();
    }
}

3.添加注解

//控制器类注解
@Api(value = "终端Controller",tags = "获取终端信息的控制器")
public class AnalogController {
  ........
}
//控制器方法注解
@ApiOperation(value = "分页获取终端的信息列表")
@ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "page", value = "分页索引", required = true, dataType = "int"),
        @ApiImplicitParam(paramType = "query", name = "size", value = "页大小", required = true, dataType = "int")
})
public Result getTerminalPage(@PathVariable("page") int page, @PathVariable("size") int size) {
    Object pageData = service.getTerminalPageData2(page, size);
    return new Result(true, StatusCode.OK, "获取终端列表", pageData);
}
//接口忽略注解可以添加在控制器类和方法上
@ApiIgnore
//响应类注解
@ApiModel(value = "响应结果最外层类")
public class Result {
    @ApiModelProperty(value = "成功失败标志,true为成功,false为失败", required = true)
    private boolean flag;
    @ApiModelProperty(value = "返回码,20000为成功,20001为失败", required = true)
    private Integer code;
    @ApiModelProperty(value = "成功或失败信息信息", required = true)
    private String message;
    @ApiModelProperty(value = "返回的具体数据可能为null,String,POJO,列表", required = false)
    private Object data;
  ......
}

4.测试
运行项目,在浏览器中输入:
http://localhost:9001/sso/swagger-ui.html
前半段:http://localhost:9001/sso/是路径每个项目都不同

可以看到SwaggerUI界面

相关文章

网友评论

      本文标题:Swagger的使用

      本文链接:https://www.haomeiwen.com/subject/ypyywqtx.html