@ControllerAdvice(annotations = {RestController.class})
@Slf4j
public class GlobalControllerAdvice {
@ExceptionHandler({Throwable.class, Error.class})
@ResponseBody
public Result error(Throwable e) {
String stackTrace = ExceptionUtil.getMessage(e);
log.error(e.getMessage(), e);
return ResultGenerator.genFailResult(stackTrace);
}
}
public class Result<T> implements Serializable {
private int code;
private String message;
private T data;
public Result() {
super();
}
public Result setCode(ResultCode resultCode) {
this.code = resultCode.getCode();
return this;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public Result setMessage(String message) {
this.message = message;
return this;
}
public T getData() {
return data;
}
public Result setData(T data) {
this.data = data;
return this;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
public static Result genFailResult(String message) {
return new Result()
.setCode(ResultCode.FAIL)
.setMessage(message);
}