当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

Special topic II on mathematical physics of the sprint strong foundation program

简单快速的数网络(网络中的网络套娃)

Oracle database basics concepts

1+1<2 ?! Interpretation of hesic papers

Lambda expression

Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022

ArcGIS 镶嵌数据集切片丢失问题处理

超硬核!华为智慧屏上的家庭相册竟可以自动精准分类?

C#程序结构预览最基础入门

Review the old and know the new -- constant renewal at normal temperature
随机推荐
Reading graph augmentations to learn graph representations (lg2ar)
MATLAB data type - character type
test
Technical dry goods | what is a big model? Oversized model? Foundation Model?
Other service registration and discovery
Solution of idea hot start failure
find_circ详细使用指南
3-wire SPI screen driving mode
直播回顾 | 子芽&CCF TF:云原生场景下软件供应链风险治理技术浅谈
小白看MySQL--windows环境安装MySQL
XML learning notes
Is it safe to open a compass account?
What is the difference between the working principle of gas-liquid slip ring and other slip rings
Target tracking shooting? Target occlusion shooting? With 1.9 billion installed petal apps, what unique features attract users?
“message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
xml学习笔记
Network in network (dolls)
大健康行业年度必参盛会,2022山东国际大健康产业博览会
Gaussian and Summary Stats
数字格式化的 js 库