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

Swagger的学习

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

Swagger的学习

一、Swagger

Swagger是一个用来定义接口标准,接口规范,同时能根据你的代码自动生成接口说明文档的一个工具。

二、构建Swagger和springboot环境

构建时碰到一个问题:

Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerException

原因是因为springboot的版本太高了,缺少swagger运行所需要的环境。

这里我将springboot的版本降到了2.5.0。

之前springboot版本是2.6.1,而swagger的版本是2.9.2 。

2.1、引入依赖
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
2.2、编写Swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(){
        return new Docket(documentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.charry.h.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                .title("Springboot整合Swagger")
                .description("Springboot整合Swagger,详细信息...")
                .version("2.0")
                .contact(new Contact("charryH","www.charry.com","charry@133.com"))
                .license("The License")
                .licenseUrl("https://www.baidu.com")
                .build());
    }
}

apis:使用什么样的方式来扫描接口。paths:扫描接口的路径。

2.3、开发Controller接口
@RestController
@RequestMapping("user")
public class UserController {

    @GetMapping("findAll")
    public Map findAll(){
        Map map = new HashMap<>();
        map.put("success","查询成功!");
        map.put("status",true);
        return map;
    }
}

2.4、访问接口界面

三、Swagger的注解 3.1、@Api

作用:用在指定接口的描述文字修饰范围:用在类上

@RestController
@RequestMapping("user")
@Api(tags = "用户接口")
public class UserController {
}

3.2、@ApiOperation

作用:用来对接口中具体方法做描述修饰范围:用在方法上

@RequestMapping("findAll")
    @ApiOperation(value = "查询所有",notes = "详细描述....")
    public Map findAll(){
}

3.3、@ApiImplicitParams

作用:用在接口中的参数进行说明修饰范围:用在方法上

 @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "姓名",dataType = "String",defaultValue = "张三"),
            @ApiImplicitParam(name = "password",value = "密码",dataType = "String",defaultValue = "157213")
    })
    public Map save(String name,String password){
}

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

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

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