The data structures returned by REST API-based projects are uniform, which facilitates interface docking and use. Therefore, there are certain requirements for the design of the resulting object:
- A result object should support both success and failure data structures;
- Be able to handle multiple types of return result objects;
- Can easily determine whether the result is a success or failure;
- Can store error codes and error messages;
The following is based on the Spring Boot project to design the corresponding unified result return object. The corresponding class is defined as ApiResponse:
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonValue; public final class ApiResponse<T> { private ErrorResponse error; private T data; private Status status; private ApiResponse(Status status, ErrorResponse error) { this.error = error; this.status = status; } private ApiResponse(Status status, T data) { this.data = data; this.status = status; } @JsonCreator public static <T> ApiResponse<T> success( @JsonProperty("status") Status status, @JsonProperty("data") T data) { return new ApiResponse<>(status, data); } @JsonCreator public static <T> ApiResponse<T> error( @JsonProperty("status") Status status, @JsonProperty("error") ErrorResponse error) { return new ApiResponse<>(status, error); } public enum Status { SUCCESS("success"), ERROR("error"); private final String status; Status(String status) { this.status = status; } @JsonValue public String getStatus() { return this.status; } } public ErrorResponse getError() { return this.error; } public T getData() { return this.data; } public Status getStatus() { return this.status; }}Copy the code
The above class has the following key points:
- The Status enumeration class is used to hold the Status so that it is easy to see whether the request was successful or failed.
- The data variable uses generics and can store objects of any type;
- Error information can be obtained through the error attribute;
The contents of the ErrorResponse class are as follows:
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; public final class ErrorResponse { public final String code; public final String message; @JsonCreator public ErrorResponse( @JsonProperty("code") String code, @JsonProperty("message") String message) { this.code = code; this.message = message; }}Copy the code
ErrorResponse has two fields: code and message. Code can be the HTTP return code or any business code. The message variable is used to describe the error message.
Based on the preceding objects, the format of the RETURNED JSON packet is as follows:
{
"status": "success",
"data": {
"task": "Write a post",
"taskStatus":"in_progress",
"tags":["writing"]
}
}
Copy the code
The error packet is as follows:
{
"status": "error",
"error": {
"code" "409",
"message" : "User with username xyz already exists"
}
}
Copy the code
Note that when serializing JSON, you need to exclude null values. For example, when an error occurs, you may not want data to be null. If you are using the Spring Boot framework, you can configure this as follows:
spring.jackson.default-property-inclusion=NON_NULL
Copy the code
If you are using Java 8 or Gauva and want to exclude optional types, use the NON_ABSENT value.
spring.jackson.default-property-inclusion=NON_ABSENT
Copy the code
About the blogger: Author of the technology book SpringBoot Inside Technology, loves to delve into technology and writes technical articles.
Public account: “program new vision”, the blogger’s public account, welcome to follow ~
Technical exchange: Please contact the weibo user at Zhuan2quan