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

Swagger前后台集成工具

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

Swagger前后台集成工具

Swagger的使用方法

文章目录

Swagger的使用方法

1.引入依赖2.配置Swagger23.访问API文档页面4.常用注解

1.引入依赖
 
            io.springfox
            springfox-boot-starter
            3.0.0
        
2.配置Swagger2
@Configuration
@EnableSwagger2
//3.0.0以上版本无需加该注解
public class Swagger2Configuration{
    @Bean
    public Docket docket(){
        
        return new Docket(documentationType.SWAGGER_2)
         .apiInfo(new ApiInfoBuilder()
                 .title("用户管理系统")
                 .description("用户管理系统接口文档")
                 .version("3.3")
                 .contact(new Contact("公司名","http://www.baidu.com","13813838438@qq.com"))
                  .build()
                 )
         .select()
          //Controller所在的包
         .api(RequestHandlerSelectors.basePackage("com.xxx.controller"))
         .build();
    }
}
3.访问API文档页面

在浏览器中访问 http://localhost:8080/projectName/swagger-ui/index.html 即可。

4.常用注解

实例写一个Controller类

@RestController
@RequestMapping("/user")
@Api(tags="用户相关操作接口")
public class UserController{
    @ApiOperation("根据用户Id进行查询")
    @ApiImplicitParam(name="id",value="用户id",requires=true,example="33",defaultValue="444") 
    @PostMapping("/selectById")
    public String selectById(Integer id){
        ...
            return "selectById";
    }
}

如果传参是一个实体类对象User,栗子:

@ApiModel
public class User{
    //对Integer等数字类型需要赋默认值,否则报错
    @ApiModelProperty(value="用户ID",example="1")
    private Integer userId;
    @ApiModelProperty(value="用户名")
    private String userName;
    @ApiModelProperty(value="用户密码")
    private String password;
    @ApiModelProperty(value="用户地址")
    private String address;
    ...(setter,getter and toString)
}

提取知识点:

@Api:用于设置模块接口名

@ApiOperation(...):用于设置功能方法名

@ApiImplicitParam:用于描述方法参数含义

其下一些参数描述:

​ ①name:形参名

​ ②value:描述含义

​ ③required:是否为必填项

​ ④example:样例值

​ ⑤defaultValue:默认值

@ApiImplicitParams:使用数组格式存储多个参数描述

@ApiModel:用在实体类上,用于实体类在API文档上的说明。

@ApiModelProperty:用于说明实体类中属性在API文档上的解释说明。

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

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

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