栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Swagger2使用------------整合SpringBoot

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swagger2使用------------整合SpringBoot

什么是swagger2


编写和维护接口文档是每个程序员的职责,根据Swagger2可以快速帮助我们编写最新的API接口文档,再也不用担心开会前仍忙于整理各种资料了,间接提升了团队开发的沟通效率。

常用注解

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
 

 swagger2怎么用? 1.导入pom依赖

    2.9.2

    io.springfox
    springfox-swagger2
    ${swagger.version}


    io.springfox
    springfox-swagger-ui
    ${swagger.version}
2.配置 SwaggerConfiguration配置类
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }
    private ApiInfo apiInfo(){
        Contact ct = new Contact("cm", "http://cm.sina.cn", "cm@sina.com");
        return new ApiInfo("我的塔吊信息测试","塔吊基础信息测试....","v1.0","http://www.bdqn.cn",ct,"学术部 2.0许可","许可连接",new ArrayList());
    }
}
3.controller层加注解 Api()

 

@Api()
@RestController
@RequestMapping("/tcinfo")
public class TcinfosCtrl {

@GetMapping("/findTcById")
@ApiOperation("按照用户传入的塔机编号 查询塔机信息")
@ApiImplicitParams({
        @ApiImplicitParam(name = "tcid",value = "吊塔编号",required = true,paramType = "query",dataType = "int")
})
public TcinfosVO findById(@RequestParam(value = "tcid")Integer tcid){
    Tcinfos tic = tcinfoStoreService.findTcByTcid(tcid);
    TcinfosVO tvo = new TcinfosVO();
    BeanUtils.copyProperties(tic,tvo);
    return tvo;
}

}

 

4 启动   浏览器输入地址 开始接口测试

http://localhost:9090/swagger-ui.html#/

//9090 用自己的端口 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1024378.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号