Idle, finished writing their own module, looked at the project framework. There was a big guy who said that a lot of companies don’t do unified exception handling and unified return handling, and my company does it now, so let’s see. First of all, let’s understand what is unified return, unified return is a specification, let’s customize this specification, data returned from the back end are in accordance with this specification, so that the front end is easier to deal with. It’s usually a class, and that class is called Result
@apiModelProperty (notes = "state ") private int code; @APIModelProperty (notes = "ApiModelProperty ") private String Message; @apiModelProperty (notes = "return content ") private T data; Public Result() {this(SUCCESS, "SUCCESS!" ); } public Result(int code, String message, T content) { this.code = code; this.message = message; this.data = content; } public Result(int code, String message) { this(code, message, null); } public static <T> Result<T> error() { return error(ERROR, "error!" ); } public static <T> Result<T> error(String msg) { return error(ERROR, msg); } public static <T> Result<T> error(int code, String msg) { return new Result<>(code, msg); } public static <T> Result<T> error(String msg, T content) { return new Result<>(ERROR, msg, content); } public static <T> Result<T> error(int code, String msg, T content) { return new Result<>(code, msg, content); } public static <T> Result<T> ok() { return new Result<>(); } public static <T> Result<T> ok(String message) { return new Result<>(SUCCESS, message); } public static <T> Result<T> ok(T content) { return new Result<>(SUCCESS, "success!" , content); } public static <T> Result<T> ok(int status, T content) { return new Result<>(status, "success!" , content); } public static <T> Result<T> ok(int status, String message, T content) { return new Result<>(status, message, content); } @Override public boolean matching() { return this.code == 0; }Copy the code
The ResponseBodyAdvice interface class is called ResponseBodyAdvice. The ResponseBodyAdvice interface class is called ResponseBody. The ResponseBodyAdvice interface class is called ResponseBodyAdvice. It has two methods, the first method, there are two parameters, we can go to know about the ah, I only use the returnType. GetGenericParameterType (), the method is to get the returned data types, so we can figure out whether returns the type of the specified by us.
boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<? >> converterType);Copy the code
This method is mainly to determine whether a second method is needed beforeBodyWrite.
If true of SUPPORTS is returned, the second method is executed. If false is returned, the second method is not executed. So the first supports handles some return values more precisely based on the location to the package.
As for the uniform treatment of the return value depends on our second method. Methods in the body is the corresponding return values, and returnType. GetGenericParameterType () to get the return value type
T beforeBodyWrite(T body, MethodParameter returnType, MediaType selectedContentType,Class<? extends HttpMessageConverter<? >> selectedConverterType,ServerHttpRequest request, ServerHttpResponse response);Copy the code
So we implement this interface class, we override these two methods, and we annotate @RestControllerAdvice(basePackages = “XXX”), and that annotation intercepts controller, and when you type in the package path, that’s the package path. The first method, which we implement mainly to determine if the return type is our canonical type, returns true if not, and executes the second method. The second method does a processing on the data, and we encapsulate it so that the return type is uniform. As follows:
if (body == null) {
return Result.ok();
}
if (body instanceof Result) {
return body;
}
if (returnType.getGenericParameterType().equals(String.class)) {
return body;
}
return Result.ok(body);
Copy the code
After completing the above steps, even if the return is empty, as long as no error is reported, there will be a status code, and the status, the above is to return the unified format.
Next is uniform exception handling, uniform exception handling, also using the @RestControllerAdvice annotation, which intercepts all controllers, and @ExceptionHandler(), which specifies the exception type. These two annotations are used. The code is as follows:
public ResponseEntity<Object> customExceptionHandler(BaseCustomException exception) {
log.error(exception.getLogMessage());
return new ResponseEntity<>(Result.error(exception.getErrorCode(), exception.getMessage()), exception.getHttpStatus());
}
Copy the code
Complete the above code so that exceptions can be thrown in our format. Customizing an exception completes the custom exception handling.Copy the code