Secondary encapsulation of element plus business components based on Vue3+Vite+TS

Secondary encapsulation of element plus business components based on Vue3+Vite+TS

Why return a unified canonical format for SpringBoot
Under the condition of acquiescence, there are three common return formats of SpringBoot:

First: return String

@GetMapping("/hello")
public String getStr(){
return "hello,javadaily";
}download
Copy code
At this time, the return value obtained by calling the interface is as follows:

hello,javadaily
Copy code
Second: return custom objects

@GetMapping("/aniaml")
public Aniaml getAniaml(){
Aniaml aniaml = new Aniaml(1,"pig");
return aniaml;
}
Copy code
At this time, the return value obtained by calling the interface is as follows:

{
"id": 1,
"name": "pig"
}
Copy code
Third: interface exception

@GetMapping("/error")
public int error(){

int i = 9/0;
return i;

}
Copy code
At this time, the return value obtained by calling the interface is as follows:

{
"timestamp": "2021-07-08T08:05:15.423+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/wrong"
}
Copy code
Based on the above conditions, if you debug the interface with the front-end developers, they will be very confused. Because we don't give them a unified format, the front-end developers don't know how to deal with the return value.

What's more, some students like Xiao Zhang like to stop packaging the results. He uses the Result object. Xiao Wang also likes to stop packaging the results, but he uses the Response object. When this situation is presented, I believe the front-end personnel will be crazy.

Therefore, we need to define a unified standard return format in our project.
download
Define return Specification Format
The return format of a specification contains at least 3 parts:

Status value: the status codes of various returned results are uniformly defined by the backend
message description: description of the result of this interface call
Data: data returned this time.
{
"status":"100",
"message": "operation succeeded",
"data":"hello,javadaily"
}
Copy code
Of course, you can also participate in other extension values as needed. For example, we added the interface call time to the return object

timestamp: interface call time
Define return object
@Data
public class ResultData<T> {
/*Result status. See resultdata for detailed status code java/
private int status;
private String message;
private T data;
private long timestamp ;
public ResultData (){

this.timestamp = System.currentTimeMillis();

}
public static ResultData success(T data) {

ResultData resultData = new ResultData<>();
resultData.setStatus(ReturnCode.RC100.getCode());
resultData.setMessage(ReturnCode.RC100.getMessage());
resultData.setData(data);
return resultData;

}
public static ResultData fail(int code, String message) {

ResultData resultData = new ResultData<>();
resultData.setStatus(code);
resultData.setMessage(message);
return resultData;

}
}download
Copy code
Define status code
public enum ReturnCode {

/**Operation victory**/
RC100(100,"Operation victory"),
/**Operation failed**/
RC999(999,"operation failed"),
/**Service current limiting**/
RC200(200,"Service opening current limiting maintenance,Please try again later!"),
/**Service degradation**/
RC201(201,"Service start degradation maintenance,Please try again later!"),
/**Hot spot parameter current limiting**/
RC202(202,"Hot spot parameter current limiting,Please try again later!"),
/**System rules are not satisfied**/
RC203(203,"The system rule does not satisfy the request,Please try again later!"),
/**Authorization rule without**/
RC204(204,"Authorization rule without,Please try again later!"),
/**access_denied**/
RC403(403,"No access,Please contact the administrator to grant permission"),
/**access_denied**/
RC401(401,"Exception when anonymous users access unauthorized resources"),
/**Abnormal service**/
RC500(500,"System exception, please try again later"),
INVALID_TOKEN(2001,"Illegal access token"),
ACCESS_DENIED(2003,"You do not have permission to access this resource"),
CLIENT_AUTHENTICATION_FAILED(1001,"Client authentication failed"),
USERNAME_OR_PASSWORD_ERROR(1002,"Wrong user name or password"),
UNSUPPORTED_GRANT_TYPE(1003, "Unsupported authentication form");
/**Custom status code**/
private final int code;
/**Custom depiction**/
private final String message;
ReturnCode(int code, String message){
    this.code = code;
    this.message = message;
}
public int getCode() {
    return code;
}
public String getMessage() {
    return message;
}

}download
Copy code
Uniform return format
@GetMapping("/hello")
public ResultData getStr(){

return ResultData.success("hello,javadaily");

}
Copy code
At this time, the return value obtained by calling the interface is as follows:

{
"status": 100,
"message": "hello,javadaily",
"data": null,
"timestamp": 1625736481648,
"httpStatus": 0
}

download

Keywords: vite

Added by loony383 on Wed, 05 Jan 2022 13:15:25 +0200