java后端定义通用异常返回对象
------------------------------------------------------------------------------------------------------------------------------------------------------------
UserController.java
------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.miaoshaproject.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.miaoshaproject.controller.viewobject.UserVO;
import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.error.EmBusinessError;
import com.miaoshaproject.response.CommonReturnType;
import com.miaoshaproject.service.UserService;
import com.miaoshaproject.service.model.UserModel;
//名为user的控制器
@Controller("user")
@RequestMapping("/user")
public class UserController extends BaseController{
@Autowired
private UserService userService;
@RequestMapping("/get")
@ResponseBody
public CommonReturnType getUser(@RequestParam(name="id")Integer id) throws BusinessException{
//调用service服务获取对应id的用户对象,给前端
UserModel userModel=userService.getUserById(id);
//若用户信息不存在
if(userModel==null) {
userModel.setEncrptPassword("111");
//throw new BusinessException(EmBusinessError.USER_NOT_EXIST);
}
//前端可视模型
UserVO userVO= convertFromModel(userModel);
return CommonReturnType.create(userVO);
}
//转化核心模型为前端可视模型
private UserVO convertFromModel(UserModel userModel) {
if(userModel==null) {
return null;
}
UserVO userVO=new UserVO();
BeanUtils.copyProperties(userModel,userVO);
return userVO;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
CommonError.java
------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.miaoshaproject.error;
public interface CommonError {
public int getErrCode();
public String getErrMsg();
public CommonError setErrMsg(String errMsg);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
EmBusinessError.java
------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.miaoshaproject.error;
public enum EmBusinessError implements CommonError{
//通用错误类型10001
PARAMETER_VALIDATION_ERROR(10001,"参数不合法"),
UNKNOWN_ERR(10002,"未知错误"),
//20000开头为用户信息相关错误
USER_NOT_EXIST(20001,"用户不存在")
;
private EmBusinessError(int errCode,String errMsg) {
this.errCode=errCode;
this.errMsg=errMsg;
}
private int errCode;
private String errMsg;
public int getErrCode() {
return this.errCode;
}
public String getErrMsg() {
return this.errMsg;
}
public CommonError setErrMsg(String errMsg) {
this.errMsg=errMsg;
return this;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
BusinessException.java
------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.miaoshaproject.error;
//包装器业务异常类
public class BusinessException extends Exception implements CommonError{
private CommonError commonError;
//直接接收EmBusinessError的参数构造业务异常
public BusinessException(CommonError commonError) {
super();
this.commonError=commonError;
}
//接收自定义errMsg构造异常
public BusinessException(CommonError commonError,String errMsg) {
super();
this.commonError=commonError;
this.commonError.setErrMsg(errMsg);
}
public int getErrCode() {
// TODO Auto-generated method stub
return this.commonError.getErrCode();
}
public String getErrMsg() {
// TODO Auto-generated method stub
return this.commonError.getErrMsg();
}
public CommonError setErrMsg(String errMsg) {
this.commonError.setErrMsg(errMsg);
return this;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
BaseController.java
------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.miaoshaproject.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.error.EmBusinessError;
import com.miaoshaproject.response.CommonReturnType;
public class BaseController {
//定义exceptionhandler解决未被控制层续收的异常
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Object handlerException(HttpServletRequest request,Exception ex) {
Map<String,Object> responseData=new HashMap<String, Object>();
if(ex instanceof BusinessException) {
BusinessException businessException=(BusinessException)ex;
responseData.put("errCode", businessException.getErrCode());
responseData.put("errMsg", businessException.getErrMsg());
}else {
responseData.put("errCode", EmBusinessError.UNKNOWN_ERR.getErrCode());
responseData.put("errMsg", EmBusinessError.UNKNOWN_ERR.getErrMsg());
}
return CommonReturnType.create(responseData,"fail");
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
CommonReturnType.java
------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.miaoshaproject.response;
public class CommonReturnType {
//请求的处理结果,“success”、“fail”
private String status;
//成功则data返回所需json数据
//失败则返回通用错误码格式
private Object data;
//通用创建方法
public static CommonReturnType create(Object result) {
return CommonReturnType.create(result,"success");
}
public static CommonReturnType create(Object result,String status) {
CommonReturnType type=new CommonReturnType();
type.setStatus(status);
type.setData(result);
return type;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------