当前位置:网站首页>Global exception handlers and unified return results
Global exception handlers and unified return results
2022-06-28 09:36:00 【Eat more porridge in the morning】
1、 Unified return result class
/** * @author * @ClassName: Result * @Description: Unified return result class Guaranteed serialization json When , If it is null The object of ,key Will disappear * @date 2021/9/18 15:32 */
@Data
public class Result<T> implements Serializable {
private Boolean success;
private Integer code;
private String message;
private T data;
public Result() {
}
private Result(Boolean success, Integer code) {
this.success = success;
this.code = code;
}
private Result(Boolean success, Integer code, T data) {
this.success = success;
this.code = code;
this.data = data;
}
private Result(Boolean success, Integer code, String message, T data) {
this.success = success;
this.code = code;
this.message = message;
this.data = data;
}
private Result(Boolean success, Integer code, String message) {
this.success = success;
this.code = code;
this.message = message;
}
public static <T> Result<T> success() {
return new Result<T>(true, ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getMessage());
}
public static <T> Result<T> successMessage(String message) {
return new Result<T>(true, ResponseEnum.SUCCESS.getCode(), message);
}
public static <T> Result<T> success(T data) {
return new Result<T>(true, ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getMessage(),data);
}
public static <T> Result<T> success(String message, T data) {
return new Result<T>(true, ResponseEnum.SUCCESS.getCode(), message, data);
}
public static <T> Result<T> error() {
return new Result<T>(false, ResponseEnum.ERROR.getCode(), ResponseEnum.ERROR.getMessage());
}
public static <T> Result<T> errorMessage(String errorMessage) {
return new Result<T>(false, ResponseEnum.ERROR.getCode(), errorMessage);
}
public static <T> Result<T> errorCodeMessage(Integer errorCode, String errorMessage) {
return new Result<T>(false, errorCode, errorMessage);
}
}
2、 Error code enumeration class
/** * @author * @ClassName: ResponseEnum * @Description: Response status code and enumeration type of prompt message * @date 2021/9/17 15:07 */
@Getter
public enum ResponseEnum {
SUCCESS(200," Successful operation "),
ERROR(500," operation failed "),
INVALID_PARAM_ERROR(400," Invalid request parameter "),
;
private Integer code;
private String message;
ResponseEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}
3、 Custom exception handling class
/** * @author * @ClassName: CustomException * @Description: Custom exception * @date 2021/9/18 15:43 */
@Data
public class CustomException extends RuntimeException {
private Integer code;
public CustomException(ResponseEnum responseEnum) {
super(responseEnum.getMessage());
this.code = responseEnum.getCode();
}
public CustomException(ResponseEnum responseEnum, Throwable throwable) {
super(responseEnum.getMessage(), throwable);
this.code = responseEnum.getCode();
}
}
4、 Global exception handling class
/** * @author * @ClassName: GlobalUniformException * @Description: Globally unified exception handling class * @date 2021/9/18 15:48 */
@RestControllerAdvice
@Slf4j
public class GlobalUniformException {
/** * Handle custom exceptions --------> Specify the exception type to be handled by the so and so method * @param e * @return */
@ExceptionHandler(CustomException.class)
public Result personalException(CustomException e){
e.printStackTrace();
return Result.errorCodeMessage(e.getCode(),e.getMessage());
}
/** * Handle exceptions other than custom exceptions * @param e * @return */
@ExceptionHandler(Exception.class)
public Result handlerOtherException(Exception e){
e.printStackTrace();
return Result.errorCodeMessage(ResponseEnum.ERROR.getCode(),ResponseEnum.ERROR.getMessage());
}
}
5、 The test returns a uniform result
/** * @ * @ClassName: HelloController * @Description: test result * @date 2021/9/18 15:48 */
@RestController
@RequestMapping("/say")
public class HelloController {
@PostMapping("/hello")
public Result sayHello(User user){
if (StringUtils.isEmpty(user.getName())) {
return Result.errorMessage(ResponseEnum.ERROR.getMessage());
}
user.setName(" Xiao Ming ");
user.setAge(20);
return Result.success(user);
}
}
Browser test results :

postman Browser test results :

6. The test throws an exception :
@RestController
@PostMapping("/say")
public class HelloController {
@RequestMapping("/hello")
public Result sayHello(User user){
User user = new User();
if (user.getName() == null) {
throw new CustomException(ResponseEnum.INVALID_PARAM_ERROR);
}
user.setName(" Xiao Ming ");
user.setAge(20);
return Result.success(user);
}
}
postman Test failure results :

Browser test failure results :

边栏推荐
- When the interviewer asks you to write binarysort in two ways
- 静态代码块永远先执行? 格局小了!!!
- For the development of short video app, the elder warned me to choose the open source code
- 股票 停牌
- 数据挖掘建模实战
- RESTful风格
- DBeaver安装与使用教程(超详细安装与使用教程)
- The digital human industry is about to break out. What is the market pattern?
- 当面试官让你用两种方式写BinarySort
- Which occupational groups are suitable for the examination
猜你喜欢

The digital human industry is about to break out. What is the market pattern?

Prototype chain JS

Basic knowledge of hard disk (head, track, sector, cylinder)

Learn how Alibaba manages the data indicator system

Data mining modeling practice

Virtual machine 14 installing win7 (Figure tutorial)

Function sub file writing

SQL 優化經曆:從 30248秒到 0.001秒的經曆
![1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定](/img/1a/162b060a6498e58278b6ca50e4953c.png)
1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定

JDBC connection database (MySQL) steps
随机推荐
The concept of "tree structure" perfectly interprets the primary and secondary of things
Is it safe to open an account for mobile phone stock speculation?
桥接模式(Bridge)
Basic knowledge of hard disk (head, track, sector, cylinder)
PMP考试重点总结九——收尾
Apache Doris becomes the top project of Apache
自定义异常类及练习
Interpretation of new products: realm launched GT neo2 Dragon Ball customized version
线程的生命周期
RESTful风格
Campus honey decoration of APP course design (e-commerce platform)
SQL 优化经历:从 30248秒到 0.001秒的经历
Calculation of stock purchase and sale expenses
剑指Offer | 斐波那契数列
How do I open an account on my mobile phone? Is it safe to open an account online now?
When the interviewer asks you to write binarysort in two ways
1182:合影效果
Boundary value analysis method for learning basic content of software testing (2)
创建多线程的方法---1创建Thread类的子类及多线程原理
自动转换之-面试题