- 首页 > 生活 > >
前后端分离开发,后端需要编写接?说明?档,会耗费?较多的时间 。swagger 是?个?于?成服务器接?的规范性?档,并且能够对接?进?测试的?具 。
作用
使用步骤
- 添加依赖
<!--swagger--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency> - 写配置类
SwaggerConfig
/** * SwaggerConfig 接口文档配置类 */@Configuration@EnableSwagger2public class SwaggerConfig {/*** 配置接口文档生成规则*/@Beanpublic Docket getDocket() {// 设置文档生成规则return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) // 设置文档信息.select()// 设置哪个包下的类需要生成文档.apis(RequestHandlerSelectors.basePackage("com.luis.fmmall.controller")).paths(PathSelectors.any()) // 定义哪些路径的接口需要生成文档.build();}/*** 设置文档信息*/private ApiInfo apiInfo() {return new ApiInfoBuilder().title("xxx接口文档").description("这里是相关描述").version("1.0").contact(new Contact("luis","https://www.cnblogs.com/luisblog","xxx@qq.com")).build();}} - 在控制器类上使用 Swagger 的注解进行相关说明
示例如下:
@RestController@RequestMapping("/user")@Api(tags = "用户管理", value = "https://www.huyubaike.com/biancheng/提供用户的登陆、注册、修改等功能") //类说明public class UserController {@Resourceprivate UserService userService;@GetMapping("/login")@ApiOperation(value = "https://www.huyubaike.com/biancheng/登陆验证", notes = "用户登陆检查") //方法名说明@ApiImplicitParams({ //参数说明@ApiImplicitParam(dataType = "string", name = "username", value = "https://www.huyubaike.com/biancheng/用户名", required = true),@ApiImplicitParam(dataType = "string", name = "password", value = "https://www.huyubaike.com/biancheng/用户密码", required = false, defaultValue = "https://www.huyubaike.com/biancheng/123")})public ResultVo login(@RequestParam("username") String name,@RequestParam(value = "https://www.huyubaike.com/biancheng/password", defaultValue = "https://www.huyubaike.com/biancheng/123") String pwd) {return userService.checkLogin(name, pwd);}} - 启动 SpringBoot 应用,访问
http://localhost:8080/swagger-ui.html
效果如下:

文章插图
常用注解说明
@Api:类注解,使用在控制器类上,对类进行说明
控制器类 UserController 示例:
@Api(tags = "用户管理", value = "https://www.huyubaike.com/biancheng/提供用户的登陆、注册、修改等功能") //类说明public class UserController {}@ApiOperation:方法注解,使用在方法上,对方法名进行说明
@ApiImplicitParam 和 @ApiImplicitParams:方法注解,使用在方法上,对方法的形参进行说明
单个形参使用 @ApiImplicitParam,多个形参使用 @ApiImplicitParams
控制器类 UserController 的 login 方法示例:
@GetMapping("/login")@ApiOperation(value = "https://www.huyubaike.com/biancheng/登陆验证", notes = "用户登陆检查") //方法名说明@ApiImplicitParams({ //参数说明@ApiImplicitParam(dataType = "string", name = "username", value = "https://www.huyubaike.com/biancheng/用户名", required = true),@ApiImplicitParam(dataType = "string", name = "password", value = "https://www.huyubaike.com/biancheng/用户密码", required = false, defaultValue = "https://www.huyubaike.com/biancheng/123")})public ResultVo login(@RequestParam("username") String name,@RequestParam(value = "https://www.huyubaike.com/biancheng/password", defaultValue = "https://www.huyubaike.com/biancheng/123") String pwd) {return userService.checkLogin(name, pwd);}
经验总结扩展阅读
-
-
-
婆婆|婆婆做手术,我从娘家借来六万,婆婆出院后跟亲戚说的话,我傻了
-
-
使用LabVIEW实现基于pytorch的DeepLabv3图像语义分割
-
发型成都有没有教设计发型原理的培训学校?哪些美发学校比较不错?
-
01时间从未会为谁而停留过 人这辈子很短,短到自己还没来得及尽孝,想一想就很难过
-
对于有了一定的年纪 不管怎么说,一份好的感情,那始终都值得期待
-
苹果电脑开机密码忘记了怎么解决 苹果笔记本忘记开机密码的解决方法
-
-
-
真正的失败就是放弃 人生就像是一场拔河比赛,每个人都在与困难博弈
-
-
-
夏天空调开睡眠模式省电还是费电 空调开睡眠模式开多少度
-
闺蜜|女人对你动了心,这五个证据最明显,大胆一点,她就是你的人
-
-
-
新生儿半夜睡不着,白天睡不醒,8个步骤让昼夜节律不混乱
-