前言:该文章只是记录自己平时的点点滴滴.来激励自己成长.长成自己喜欢的人!
环境:
- 电脑 Mac 10.13.1
- IntelliJ IDEA 2018.2 (Ultimate Edition)
- Spring boot 2.0.1.RELEASE
实现方案:
- 各个微服务返回统一对象[异常,以及正常都返回一个bean]
- 可以通过某个异常定位到微服务
实现细节:
- @RestControllerAdvice @ExceptionHandler(value = Exception.class)
- BaseException
- IExceptionEnum -> TallyException
- ResultModel
@RestControllerAdvice @ExceptionHandler(value = Exception.class)
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ResultModel handler(Exception e){
log.info("异常进入 ... 转换ResultModel ... ");
if (e instanceof BaseException){
BaseException iBaseException = (BaseException)e;
return ResultModel.fail(iBaseException.getCode(), iBaseException.getMessage());
} else {
return ResultModel.fail(88888888,"88888888");
}
}
}
BaseException
public class BaseException extends RuntimeException{
@Setter
@Getter
private int code;
public BaseException(IExceptionEnum iExceptionEnum){
super(iExceptionEnum.getMessage());
this.code = iExceptionEnum.getCode();
}
}
IExceptionEnum -> TallyException
public interface IExceptionEnum {
/**code(key) */
int getCode();
/** message */
String getMessage();
}
public class TallyExceptionEnum {
public enum SYNC_EXCEPTION implements IExceptionEnum {
SYNC_PROCESSING (2001,"处理中,请稍后!!!"),
SYNC_FAIL (2002,"处理失败,请重试!!!"),
SYNC_UPD_ACCOUNTDETAIL (2006,"账户异常,请查证!!!");
@Getter
private int code;
@Getter
private String message;
SYNC_EXCEPTION(int code, String message){
this.code = code;
this.message = message;
}
}
}
ResultModel
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultModel {
public enum RESULT_STATUS_ENUM implements ICommonEnum {
SUCCEED_CODE_VALUE (1000,"接口调用成功","1000");
@Getter
private String code;
@Getter
private Integer key;
@Getter
private String value;
RESULT_STATUS_ENUM(int key, String value,String code){
this.key = key;
this.value = value;
this.code = code;
}
}
/**
* 状态码
*/
private int code;
/**
* 状态码描述
*/
private String message;
/**
* 返回内容
*/
private Object data;
/**
* 返回分页参数
*/
private Object page;
private static String typePage = "typePage";
private static String typeData = "typeData";
private ResultModel(int code,String message) {
this.code = code;
this.message = message;
}
private ResultModel(Object content, String type) {
this.code = RESULT_STATUS_ENUM.SUCCEED_CODE_VALUE.getKey();
this.message = RESULT_STATUS_ENUM.SUCCEED_CODE_VALUE.getValue();
if (typePage.equals(type)){
this.page = content;
}
if (typeData.equals(type)){
this.data = content;
}
}
public static ResultModel page(Object content) {
return new ResultModel(content, typePage);
}
public static ResultModel data(Object content) {
return new ResultModel(content, typeData);
}
public static ResultModel fail(int code,String message) {
return new ResultModel(code,message);
}
}
Demo
@RestController
@RequestMapping(value = "/tally")
@Slf4j
public class TallyController extends BaseController {
/**
* 异常demo
* @author nmnl
* @version 1.0.0
* @date 2018-08-20 14:32:26
*/
@PostMapping(value = "/rexception")
public ResultModel RException(){
throw new BaseException(TallyExceptionEnum.SYNC_EXCEPTION.SYNC_PROCESSING);
}
}
注: pom如下
<dependencies>
<!-- bean set / get model -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>