Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative living
Writing in the front
Every interface, every company, every project should have its own uniform style of return, this is the agreement with the front end, this is the “face” of each set of code. Crucial.
Below we take a look at how we usually use SAO operation.
ResultBO is returned uniformly
Without further ado, get right to the code
/** * HTTP response data structure *@author yn
* @date2021/10/23. * /
public class ResultBO<T> implements Serializable {
/ / fail
public static final int ERROR = 0;
/ / success
public static final int SUCCESS = 1;
// Return the result identifier
private Integer completeCode;
// Business identifier
private String reasonCode;
// Business error log or success code
private String reasonMessage;
// Business parameter returns (any type)
private T data;
/** * Do not directly new objects * use builder mode */
private ResultBO(Builder<T> builder) {
this.reasonCode = builder.reasonCode;
this.reasonMessage = builder.reasonMessage;
this.completeCode = builder.completeCode;
this.data = builder.data;
}
@Deprecated
public ResultBO(a){}public Integer getCompleteCode(a) {
return completeCode;
}
public String getReasonCode(a) {
return reasonCode;
}
public String getReasonMessage(a) {
return reasonMessage;
}
public T getData(a) {
return data;
}
@Deprecated
public ResultBO<T> setCompleteCode(Integer completeCode) {
this.completeCode = completeCode;
return this;
}
@Deprecated
public ResultBO<T> setReasonCode(String reasonCode) {
this.reasonCode = reasonCode;
return this;
}
@Deprecated
public ResultBO<T> setReasonMessage(String reasonMessage) {
this.reasonMessage = reasonMessage;
return this;
}
@Deprecated
public ResultBO<T> setData(T data) {
this.data = data;
return this;
}
public boolean isResponseOk(a){
return this.getCompleteCode() ! =null && this.getCompleteCode() == SUCCESS;
}
@Override
public String toString(a) {
return "ResultBO{" +
"completeCode=" + completeCode +
", reasonCode='" + reasonCode + ' '' + ", reasonMessage='" + reasonMessage + ''' + ", data=" + (data == null ? "null" : data.toString()) + '}'; } public static ResultBO responseFail(String s) { return ResultBO.Builder.init().setFailMessage(s).build(); } public static ResultBO ResultBOEnum(ResultBOEnum rresultBOEnum) { return ResultBO.Builder.init().setResultBOEnum(resultBOEnum).build(); } public static ResultBO responseOK() { return ResultBO .Builder .init() .setResultBOEnum(ResultBOEnum.SUCCESS) .build(); } public static class Builder
{ private Integer completeCode; private String reasonCode; private String reasonMessage; private K data; private Builder(ResultBOEnum resultBOEnum){ if (resultBOEnum == ResultBOEnum.SUCCESS){ this.completeCode = SUCCESS; } else { this.completeCode = ERROR; } this.reasonCode = resultBOEnum.getCode(); this.reasonMessage = resultBOEnum.getMessage(); } static public
Builder
init() { return new Builder<>(ResultBOEnum.SUCCESS); } public Builder
setCompleteCode(int code) { this.completeCode = code; return this; } public Builder
setReasonCode(String code) { this.reasonCode = code; return this; } public Builder
setReasonMessage(String msg) { this.reasonMessage = msg; return this; } public Builder
setFailMessage(String msg) { this.reasonMessage = msg; this.completeCode = ERROR; return this; } public Builder
setResultBOEnum(ResultBOEnum resultBOEnum) { if (resultBOEnum == ResultBOEnum.SUCCESS){ this.completeCode = SUCCESS; } else { this.completeCode = ERROR; } this.reasonMessage = resultBOEnum.getMessage(); this.reasonCode = resultBOEnum.getCode(); return this; } public Builder
setData(K k) { this.data = k; return this; } public ResultBO
build() { return new ResultBO<>(this); }}}
Copy the code
The code is simple to use and can be redeveloped. Uniform Return Code We have adopted the return code set out in the Ali Statute. Look at the below
/** * ResultBO Return code * @author yn */ public enum ResultBOEnum {/** * Code table comments * A service error, B system error, C third-party call error * The first two digits are service errors, and the last three digits are error codes. 1 From start to 9999,0 is reserved bit * service code table * 01: user * 04: coupon *..... * / SUCCESS (" 000000 ", "SUCCESS"), / * * * user clock * / ERROR_A01001 (" A01001 ", "the user does not exist"), the coupon code / * * * * / ERROR_A04001 (" A04001." "Activity has ended "), ERROR_A04002("A04002"," coupon is not available "), ERROR_A04003("A04003"," SORRY, all the coupons are gone! Come earlier next time. "),........ /** * System exception code table */ ERROR_B00001("B00001"," system execution exception "), ERROR_B00002("B00002"," system execution timeout "); private String code; private String message; ResponseBOEnum(String code, String message) { this.code = code; this.message = message; } public String getCode() { return this.code; } public String getMessage() { return this.message; }}Copy the code
The combination of the two makes your code look more professional and your interface more robust, so let’s see how it works.
@requestMapping (value = "testApi") public ResultBO testApi(@requestParam ("userId") Integer userId){ User user = userService.findById(userId); Return resultbo.responseok (); if(null == user){return resultbo.responsefail (" user does not exist ")} return resultbo.responseok (); }Copy the code
conclusion
Unified return is customized according to your own business, not completely suitable, can only be said to be generally applicable
overtones
Thank you for reading, if you feel that you have learned something, please like, follow. Also welcome to have a question we comment below exchange
Come on! See you next time!
To share with you a few I wrote in front of a few SAO operation
Talk about different strategy patterns (Bookmarks)
Copy object, this operation is a little SAO!