企业网站用免费程序,做论文查重网站代理能赚到钱吗,推广软件哪个好,wordpress装插件目录
1 环境整合配置
2 Swagger2 常⽤注解说明
2.1 Api
2.2 ApiOperation
2.3 ApiImplicitParams
2.4 ApiResponses
2.5 ApiModel
3 用户模块注解配置
3.1 Controller 使用注解
3.2 JavaBean 使用注解
4 Swagger2 接⼝⽂档访问 由于 Spring Boot 能够快速开发、便捷… 目录
1 环境整合配置
2 Swagger2 常⽤注解说明
2.1 Api
2.2 ApiOperation
2.3 ApiImplicitParams
2.4 ApiResponses
2.5 ApiModel
3 用户模块注解配置
3.1 Controller 使用注解
3.2 JavaBean 使用注解
4 Swagger2 接⼝⽂档访问 由于 Spring Boot 能够快速开发、便捷部署等特性通常在使⽤ Spring Boot 构建 Restful 接⼝应⽤ 时考虑到多终端的原因这些终端会共⽤很多底层业务逻辑因此我们会抽象出这样⼀层来同时服务于 多个移动端或者Web 前端。对于不同的终端公⽤⼀套接⼝ API 时对于联调测试的时候就需要知道后端 提供的接⼝ API列表⽂档对于服务端开发⼈员来说就需要编写接⼝⽂档描述接⼝的调⽤地址、参数 结果等这⾥借助第三⽅构建⼯具 Swagger2 来实现 API ⽂档⽣成功能。
1 环境整合配置
pom.xml 依赖添加
dependencygroupIdio.springfox/groupIdartifactIdspringfox-swagger2/artifactIdversion2.9.2/version
/dependency
dependencygroupIdio.springfox/groupIdartifactIdspringfox-swagger-ui/artifactIdversion2.9.2/version
/dependency
配置类添加
Configuration
EnableSwagger2
public class Swagger2 {Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage(com.xxxx.springboot.controller)).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title(⽤户管理接⼝API⽂档).version(1.0).build();}
}2 Swagger2 常⽤注解说明
2.1 Api
Api⽤在请求的类上说明该类的作⽤tags说明该类的作⽤
Api(tagsAPP⽤户注册Controller)
2.2 ApiOperation
ApiOperation⽤在请求的⽅法上说明⽅法的作⽤value说明⽅法的作⽤notes⽅法的备注说明
ApiOperation(value⽤户注册,notes⼿机号、密码都是必填项年龄是选填项但必须是数
字)
2.3 ApiImplicitParams
ApiImplicitParams⽤在请求的⽅法上包含⼀组参数说明ApiImplicitParam⽤在 ApiImplicitParams 注解中指定⼀个请求参数的配置信息 name参数名value参数的汉字说明、解释required参数是否必须传paramType参数放在哪个地⽅· header -- 请求参数的获取RequestHeader· query -- 请求参数的获取RequestParam· path⽤于restful接⼝-- 请求参数的获取PathVariable· body不常⽤· form不常⽤ dataType参数类型默认String其它值dataTypeInteger defaultValue参数的默认值
ApiImplicitParams({ApiImplicitParam(namemobile,value⼿机
号,requiredtrue,paramTypeform),ApiImplicitParam(namepassword,value密
码,requiredtrue,paramTypeform),ApiImplicitParam(nameage,value年
龄,requiredtrue,paramTypeform,dataTypeInteger)
})2.4 ApiResponses
ApiResponses⽤于请求的⽅法上表示⼀组响应ApiResponse⽤在ApiResponses中⼀般⽤于表达⼀个错误的响应信息code数字例如400message信息例如请求参数没填好response抛出异常的类
ApiOperation(value select请求, notes 多个参数多种的查询参数类型)
ApiResponses({ApiResponse(code400, message请求参数没填好),ApiResponse(code404, message请求路径没有或⻚⾯跳转路径不对)
})
2.5 ApiModel
ApiModel⽤于响应类上表示⼀个返回响应数据的信息这种⼀般⽤在post创建的时候使⽤RequestBody这样的场景请求参数⽆法使⽤ApiImplicitParam注解进⾏描述的时候ApiModelProperty⽤在属性上描述响应类的属性
ApiModel(description 返回响应数据)
public class RestMessage implements Serializable{ApiModelProperty(value 是否成功)private boolean successtrue;ApiModelProperty(value 返回对象)private Object data;ApiModelProperty(value 错误编号)private Integer errCode;ApiModelProperty(value 错误信息)private String message;/* getter/setter */
}
3 用户模块注解配置
3.1 Controller 使用注解
GetMapping(user/{userName})
ApiOperation(value 根据⽤户名查询⽤户记录)
ApiImplicitParam(name userName,value 查询参数,required true,paramTypepath)
public User queryUserByUserName(PathVariable String userName){return userService.queryUserByUserName(userName);
}
ApiOperation(value 根据⽤户id查询⽤户记录)
ApiImplicitParam(name userId,value 查询参数,required true,paramType
path)
GetMapping(user/id/{userId})
public User queryUserByUserId(PathVariable Integer userId,
HttpServletRequest request){return userService.queryUserByUserId(userId);
}
GetMapping(user/list)
ApiOperation(value 多条件查询⽤户列表记录)
public PageInfoUser list(UserQuery userQuery){return userService.queryUserByParams(userQuery);
}
PutMapping(user)
ApiOperation(value ⽤户添加)
ApiImplicitParam(name user,value ⽤户实体类,dataType User)
public ResultInfo saveUser(RequestBody User user){ResultInfo resultInfonew ResultInfo();try {userService.saveUser(user);} catch (ParamsException e) {e.printStackTrace();resultInfo.setCode(e.getCode());resultInfo.setMsg(e.getMsg());}catch (Exception e) {e.printStackTrace();resultInfo.setCode(300);resultInfo.setMsg(记录添加失败!);}return resultInfo;
}
PostMapping(user)
ApiOperation(value ⽤户更新)
ApiImplicitParam(name user,value ⽤户实体类,dataType User)
public ResultInfo updateUser(RequestBody User user){ResultInfo resultInfonew ResultInfo();try {userService.updateUser(user);} catch (ParamsException e) {e.printStackTrace();resultInfo.setCode(e.getCode());resultInfo.setMsg(e.getMsg());}catch (Exception e) {e.printStackTrace();resultInfo.setCode(300);resultInfo.setMsg(记录更新失败!);}return resultInfo;
}
PutMapping(user/{userId})
ApiOperation(value 根据⽤户id删除⽤户记录)
ApiImplicitParam(name userId,value 查询参数,required true,paramType
path)
public ResultInfo deleteUser(PathVariable Integer userId){ResultInfo resultInfonew ResultInfo();try {userService.deleteUser(userId);} catch (ParamsException e) {e.printStackTrace();resultInfo.setCode(e.getCode());resultInfo.setMsg(e.getMsg());}catch (Exception e) {e.printStackTrace();resultInfo.setCode(300);resultInfo.setMsg(记录删除失败!);}return resultInfo;
}
3.2 JavaBean 使用注解
User.java
ApiModel(description 响应结果-⽤户信息)
public class User {ApiModelProperty(value ⽤户id,example 0)private Integer id;ApiModelProperty(value ⽤户名)private String userName;ApiModelProperty(value ⽤户密码)private String userPwd;/*省略get|set*/
}UserQuery.java
ApiModel(description ⽤户模块条件查询类)
public class UserQuery {ApiModelProperty(value 分⻚⻚码,example 1)private Integer pageNum 1;ApiModelProperty(value 每⻚⼤⼩,example 10)private Integer pageSize 10;ApiModelProperty(value ⽤户名)private String userName;/*省略get|set*/}
ResultInfo.java
ApiModel(description 响应结果 - Model信息)
public class ResultInfo {ApiModelProperty(value 响应状态码,example 200)private Integer code200;ApiModelProperty(value 响应消息结果)private String msg success;ApiModelProperty(value 响应具体结果信息)private Object result;/*省略get|set*/
}4 Swagger2 接⼝⽂档访问 启动⼯程浏览器访问 http://localhost:8080/springboot_mybatis/swagger-ui.html 文章转载自: http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.xirfr.cn.gov.cn.xirfr.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.tdmgs.cn.gov.cn.tdmgs.cn http://www.morning.prhfc.cn.gov.cn.prhfc.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.rynqh.cn.gov.cn.rynqh.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.irqlul.cn.gov.cn.irqlul.cn http://www.morning.cokcb.cn.gov.cn.cokcb.cn http://www.morning.ydnxm.cn.gov.cn.ydnxm.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.ygxf.cn.gov.cn.ygxf.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.dgng.cn.gov.cn.dgng.cn http://www.morning.trjr.cn.gov.cn.trjr.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.qsfys.cn.gov.cn.qsfys.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.gwsdt.cn.gov.cn.gwsdt.cn http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.kfysh.com.gov.cn.kfysh.com http://www.morning.rkxqh.cn.gov.cn.rkxqh.cn http://www.morning.mumgou.com.gov.cn.mumgou.com http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.tkcz.cn.gov.cn.tkcz.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.jpmcb.cn.gov.cn.jpmcb.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn http://www.morning.ktfnj.cn.gov.cn.ktfnj.cn http://www.morning.xgxbr.cn.gov.cn.xgxbr.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.qsy41.cn.gov.cn.qsy41.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn