当前位置:网站首页>全局异常处理器与统一返回结果
全局异常处理器与统一返回结果
2022-06-28 09:15:00 【多喝清晨的粥】
1、统一返回结果类
/** * @author * @ClassName: Result * @Description: 统一返回结果类 保证序列化json的时候,如果是null的对象,key也会消失 * @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、错误码枚举类
/** * @author * @ClassName: ResponseEnum * @Description: 响应状态码以及提示消息的枚举类型 * @date 2021/9/17 15:07 */
@Getter
public enum ResponseEnum {
SUCCESS(200,"操作成功"),
ERROR(500,"操作失败"),
INVALID_PARAM_ERROR(400,"无效的请求参数"),
;
private Integer code;
private String message;
ResponseEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}
3、自定义异常处理类
/** * @author * @ClassName: CustomException * @Description: 自定义异常 * @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、全局异常处理类
/** * @author * @ClassName: GlobalUniformException * @Description: 全局统一异常处理类 * @date 2021/9/18 15:48 */
@RestControllerAdvice
@Slf4j
public class GlobalUniformException {
/** * 处理自定义异常-------->指定某某方法要处理的异常类型 * @param e * @return */
@ExceptionHandler(CustomException.class)
public Result personalException(CustomException e){
e.printStackTrace();
return Result.errorCodeMessage(e.getCode(),e.getMessage());
}
/** * 处理自定义异常之外的其他异常 * @param e * @return */
@ExceptionHandler(Exception.class)
public Result handlerOtherException(Exception e){
e.printStackTrace();
return Result.errorCodeMessage(ResponseEnum.ERROR.getCode(),ResponseEnum.ERROR.getMessage());
}
}
5、测试返回统一结果
/** * @ * @ClassName: HelloController * @Description: 测试结果 * @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("小明");
user.setAge(20);
return Result.success(user);
}
}
浏览器测试结果:

postman浏览器测试结果:

6.测试抛出异常:
@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("小明");
user.setAge(20);
return Result.success(user);
}
}
postman测试失败结果:

浏览器测试失败结果:

边栏推荐
- Music website design based on harmonyos (portal page)
- Understanding the IO model
- Two interview demo
- Chrome devtools
- Basic operation of PMP from applying for the exam to obtaining the certificate, a must see for understanding PMP
- A strange execution plan. One table in the association query is not associated with any other tables
- 1181:整数奇偶排序
- Which is a better ERP management system for electronic component sales?
- 如何实现基于 RADIUS 协议的双因子认证 MFA?
- [share OpenGL tutorial]
猜你喜欢

Using transform:scale causes the page mouse hover event to disappear

Why does select * lead to low query efficiency?

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

STL -- binder

MATLAB小技巧(20)矩阵分析--主成分回归

The Cassandra cluster reinstalls and starts from the node. An error is reported. There is an existing solution

买卖股票费用计算

rman備份報ORA-19809 ORA-19804

Rman Backup Report Ora - 19809 Ora - 19804

For the development of short video app, the elder warned me to choose the open source code
随机推荐
Scenario method and error recommendation method for learning basic content of software testing (2)
Chrome devtools
Protection range and optimization of motor protector for hoist equipment
1. Kimball dimension modeling of data warehouse: what is a fact table?
Test cases for learning the basic content of software testing (II)
spark的资源调度和任务调度
Static page of pinyougou mall
静态代码块永远先执行? 格局小了!!!
Common test method used by testers --- orthogonal method
怎样在手机上开户?现在网上开户安全么?
From knowledge to wisdom: how far will the knowledge map go?
Stock suspension
Copy & Deepcopy
手机买同业存单基金开户选哪家证券公司比较好,比较安全呢
Interpretation of new products: realm launched GT neo2 Dragon Ball customized version
Which occupational groups are suitable for the examination
STL -- binder
Play SFTP upload file
Is it safe to open an account for online stock speculation?
Power data