当前位置:网站首页>Encapsulation of unified result set
Encapsulation of unified result set
2022-06-27 00:51:00 【Piglets are losing weight】
Unified return result set
1.
View the information of the unified returned results : Generally at least 4 individual
Success // Whether it is right boolean
Message // Returned message
Code // Status code
Data // Data
2. Specific steps
1、 First create an enumeration , Create variables that return success and failure ;
2、 Create return object class ;
a) It is recommended to have one OK Methods ,error Method is convenient to call ;
b) Self defined message,success,code,data Methods
3. Examples of enumeration classes are as follows :
@Getter
public enum ResultCodeEnum {
SUCCESS(true, 20000," success "),
UNKNOWN_REASON(false, 20001, " Unknown error "),
BAD_SQL_GRAMMAR(false, 21001, "sql Grammar mistakes "),
JSON_PARSE_ERROR(false, 21002, "json Parsing exceptions "),
PARAM_ERROR(false, 21003, " parameter is incorrect "),
FILE_UPLOAD_ERROR(false, 21004, " File upload error "),
EXCEL_DATA_IMPORT_ERROR(false, 21005, "Excel Data import error ");
private Boolean success;
private Integer code;
private String message;
private ResultCodeEnum(Boolean success, Integer code, String message) {
this.success = success;
this.code = code;
this.message = message;
}
}
4. Unified return object class Examples are as follows :
@Data
@ApiModel(value = " Global unified return results ")
public class R {
@ApiModelProperty(value = " The success of ")
private Boolean success;
@ApiModelProperty(value = " Return code ")
private Integer code;
@ApiModelProperty(value = " Return message ")
private String message;
@ApiModelProperty(value = " Return the data ")
private Map<String, Object> data = new HashMap<String, Object>();
private R(){}
public static R ok(){
R r = new R();
r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
r.setCode(ResultCodeEnum.SUCCESS.getCode());
r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
return r;
}
public static R error(){
R r = new R();
r.setSuccess(ResultCodeEnum.UNKNOWN_REASON.getSuccess());
r.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode());
r.setMessage(ResultCodeEnum.UNKNOWN_REASON.getMessage());
return r;
}
public static R setResult(ResultCodeEnum resultCodeEnum){
R r = new R();
r.setSuccess(resultCodeEnum.getSuccess());
r.setCode(resultCodeEnum.getCode());
r.setMessage(resultCodeEnum.getMessage());
return r;
}
public R success(Boolean success){
this.setSuccess(success);
return this;
}
public R message(String message){
this.setMessage(message);
return this;
}
public R code(Integer code){
this.setCode(code);
return this;
}
public R data(String key, Object value){
this.data.put(key, value);
return this;
}
public R data(Map<String, Object> map){
this.setData(map);
return this;
}
}
5. Test the following :
/*
* Query methods
* */
@ApiOperation(value = "person1 Query data ")
@ResponseBody
@GetMapping("/selectOnePerson/{id}")
public R selectOnePerson(@ApiParam(name="id", value = "personID", required = true) @PathVariable("id") Integer id) {
try {
Person person = personServer.selectOne(id);
return R.ok().data("person",person);
} catch (Exception e) {
return R.error();
}
}
边栏推荐
- 如何通俗易懂的描述机器学习的流程?
- Big guys talk about the experience sharing of the operation of the cutting-edge mindspore open source community. Come up with a small notebook!
- Simulation of delta variant strain of novel coronavirus (mindsponge application)
- 当Transformer遇见偏微分方程求解
- 2022年地理信息系统与遥感专业就业前景与升学高校排名选择
- 【Mysql】时间字段默认设置为当前时间
- matlab数据类型 —— 字符型
- 目前哪个证券公司炒股开户是最好最安全的?
- 一键加速索尼相机SD卡文件的复制操作,文件操作批处理教程
- Deep learning method for solving mean field game theory problems
猜你喜欢
当Transformer遇见偏微分方程求解
When transformer encounters partial differential equation solution
Memorizing byte order of big and small end
Moher College - SQL injection vulnerability test (error reporting and blind note)
kubernetes可视化界面dashboard
滑环选型选购时需要注意的技巧
新型冠状病毒变异Delta毒株的模拟(MindSPONGE应用)
Lambda expression
Oracle database basics concepts
In depth understanding of UDP in the transport layer and the use of UDP in sockets
随机推荐
指南针开户安全的吗?
Simulation of delta variant strain of novel coronavirus (mindsponge application)
Is it safe for CITIC Securities Commission to open an online account and speculate in stocks
Using physical information neural network to solve hydrodynamics equations
目前哪个证券公司炒股开户是最好最安全的?
JS library for number formatting
超硬核!华为智慧屏上的家庭相册竟可以自动精准分类?
Solution of idea hot start failure
[vscode] setting sync, a plug-in for synchronizing extensions and settings
From bitmap to bloom filter, C # implementation
简单快速的数网络(网络中的网络套娃)
07 | workflow design: how to design a reasonable multi person development mode?
Memorizing byte order of big and small end
Technical dry goods | what is a big model? Oversized model? Foundation Model?
XML learning notes
大健康行业年度必参盛会,2022山东国际大健康产业博览会
MySQL之账号管理、建库以及四大引擎+案例
At present, which securities company is the best and safest to open an account for stock speculation?
统计无向图中无法互相到达点对数[经典建邻接表+DFS统计 -> 并查集优化][并查集手册/写的详细]
kubeadm创建kubernetes集群