封装 返回数据 统一异常管理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z_demon801/article/details/80775750

我们返回的数据格式 可能包含错误信息,可能包含对象等,那么我们需要将其封装成一个统一的数据格式

{
	code: "",    // 错误码
	mes: "",     // 提示信息
	data: null   // 具体内容
}

VO类

package com.example.utils;

/**
 * http请求返回的最外层对象
 */
public class ResultVO<T> {

	/** 错误码. */
	private Integer code;

	/** 提示信息. */
	private String msg;

	/** 具体内容. */
	private T 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;
	}

}

VOUtil

package com.example.utils;

import com.example.handler.ResultEnum;

/**
 * ResultVO工具类
 */
public class ResultVOUtil {

	private ResultVOUtil() {
		throw new IllegalStateException("Utility class");
	}
	
	public static <T> ResultVO<T> success(T object) {
		ResultVO<T> resultVO = new ResultVO<>();
		resultVO.setData(object);
		resultVO.setCode(0);
		resultVO.setMsg("成功");
		return resultVO;
	}
	
	public static <T> ResultVO<T> success() {
		return success(null);
	}
	
	public static <T> ResultVO<T> error(Integer code, String msg) {
		ResultVO<T> resultVO = new ResultVO<>();
		resultVO.setCode(code);
		resultVO.setMsg(msg);
		return resultVO;
	}
	
	public static <T> ResultVO<T> error(ResultEnum resultEnum) {
		ResultVO<T> resultVO = new ResultVO<>();
		resultVO.setCode(resultEnum.getCode());
		resultVO.setMsg(resultEnum.getMessage());
		return resultVO;
	}
}

ResultEnum

package com.example.handler;

/**
 * 返回信息枚举
 */
public enum ResultEnum {

	SUCCESS(0, "成功"),

	SAVE_ERROR(1, "保存失败!"),

	PARAM_ERROR(2, "参数错误!"),

	SMS_ERROR(3, "短信错误!"),

	SMS_TIMED_OUT_ERROR(4, "短信超时或验证码失效!"),

	SMS_VERIFY_ERROR(5, "验证码错误!"),

	EXISTED_USER(6, "该用户已存在!"),

	ORDER_GROUPOUT_ERROR(7, "xxx"),

	ORDER_USERREPEAT_ERROR(8, "您已参与该行程"),

	ORDER_TOTAL_ERROR(9, "总价不正确,请联系客服"),

	DELETE_ERROR(10, "删除失败");

	private Integer code;

	private String message;
	
	public Integer getCode() {
		return code;
	}

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

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

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

异常捕获类

package com.example.handler;

/***
 * 异常处理类
 */
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.utils.ResultVO;
import com.example.utils.ResultVOUtil;

@ControllerAdvice
public class ExceptionHandle {

	@ResponseBody
	@ExceptionHandler(value = ExceptionHandleMes.class)
	public ResultVO<Object> hander(ExceptionHandleMes e) {
		return ResultVOUtil.error(e.getCode(), e.getMessage());
	}
}

通用error类  

package com.example.handler;

/**
 * 通用error类  
 * RuntimeException 会进行事务回滚 但是Exception不会
 */
public class ExceptionHandleMes extends RuntimeException {

private static final long serialVersionUID = 1L;
	
	private final Integer code;

	public Integer getCode() {
		return code;
	}

	public ExceptionHandleMes(Integer code, String message) {
		super(message);
		this.code = code;
	}
	
	/***
	 * 自定义错误类
	 * @param resultEnum
	 */
	public ExceptionHandleMes(ResultEnum resultEnum) {
		super(resultEnum.getMessage());
		this.code = resultEnum.getCode();
	}
}

使用

package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.model.UserInfo;
import com.example.domain.service.IUserInfoService;
import com.example.handler.ExceptionHandleMes;
import com.example.handler.ResultEnum;
import com.example.utils.ResultVO;
import com.example.utils.ResultVOUtil;

@RestController
@RequestMapping("/welcome")
public class WelcomeController {

	@Autowired
	private IUserInfoService iUserInfoService;
	
	@RequestMapping("/user")
	public ResultVO<UserInfo> user() throws Exception {
		
		UserInfo user = iUserInfoService.getUserById(917);
		
		try {
			int i = 1 / 0;
		} catch (Exception e) {
			throw new ExceptionHandleMes(ResultEnum.PARAM_ERROR);
		}
		return ResultVOUtil.success(user);
	}
}

猜你喜欢

转载自blog.csdn.net/z_demon801/article/details/80775750
今日推荐