Result类,公共返回类创建

package com.deng.hongbao.base;
/**
* 先构建一个格式确定下来的Result类
* 然后构建一个常用的枚举类,内容可以根据项目自定义,查看ResultEnum
* 然后就是写一个返回的工具类ResultUtil
* 最后就是应用了,在Controller里边调用ResultUtil就可以了
*
* 还要考虑抛异常情况,就是当条件满足时,直接把信息抛给浏览器,比如未满18岁,返回浏览器"未成年"
* 建立一个handle包,建立一个ExceptionHandle类(或者XXServiceException类)
*再建立一myException包,建立一个serviceException类继承RuntimeException类

* */
public class Result<T> {
private Integer code;
private String msg;
private T data;

public Result() {
super();
}

public Result(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

@Override
public String toString() {
return "Result{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}

//------------------------------------

package com.deng.hongbao.base;

public enum ResultEnum {
//这里是可以自己定义的,方便与前端交互即可
UNKNOWN_ERROR(-1,"未知错误"),
SUCCESS(0,"成功"),
USER_NOT_EXIST(1,"用户不存在"),
USER_IS_EXISTS(2,"用户已存在"),
DATA_IS_NULL(3,"数据为空"),
DENG_1(999,"发送人为空1111"),// 测试
;
private Integer code;
private String msg;


ResultEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}

public Integer getCode() {
return code;
}

public String getMsg() {
return msg;
}
}

//-------------------------------------
package com.deng.hongbao.util;

import com.deng.hongbao.base.Result;
import com.deng.hongbao.base.ResultEnum;

public class ResultUtil {
/**成功且带数据**/
public static Result success(Object object){
Result result = new Result();
result.setCode(ResultEnum.SUCCESS.getCode());
result.setMsg(ResultEnum.SUCCESS.getMsg());
result.setData(object);
return result;
}
/**成功但不带数据**/
public static Result success(){

return success(null);
}
/**失败**/
public static Result error(Integer code,String msg){
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
}

//----------------------------------------------------------------

package com.deng.hongbao.handle;

import com.deng.hongbao.MyException.ServiceException;
import com.deng.hongbao.base.Result;
import com.deng.hongbao.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandle {
private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

@ExceptionHandler(value = Exception.class)//对哪个异常类进行捕获
@ResponseBody
public Result handle(Exception e) {

if (e instanceof ServiceException) {
ServiceException serviceException = (ServiceException) e;
return ResultUtil.error(serviceException.getCode(), serviceException.getMessage());
} else {
// 如果是系统异常或其它异常,记录在日志
logger.error("[系统异常] {}", e);
return ResultUtil.error(-1, "未知错误!");
}
}

// 成功用ResultUtil抛出异常,但问题来了,这个异常类Exception,不能自定义ResultUtil的code,所以要自定义一个Exception
}

// ----------------------------------------------------------

package com.deng.hongbao.MyException;

import com.deng.hongbao.base.ResultEnum;

public class ServiceException extends RuntimeException {

// 继承RuntimeException异常可以事物回滚,Exception只负责抛出,但不回滚事物

private Integer code;

public ServiceException(Integer code, String msg) {
super(msg);// 由于RuntimeException继承了Exception,本身会有个msg文件,所以直接调用
this.code = code;
}

public ServiceException(ResultEnum resultEnum) {
super(resultEnum.getMsg());
this.code = resultEnum.getCode();
}

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}
}
posted @ 2020-07-26 17:46  火源  阅读(2941)  评论(0编辑  收藏  举报