在写代码的时候,难免遇见异常类处理的时候,这个时候,在处理自己的业务异常的时候,需要自己封装一个异常类,用来捕获业务异常,当然代码异常就是错误了,需要进行修改代码了,要是业务异常有可能因为用户操作等问题,比如登录的时候,出现的用户名密码错误等问题,需要提示用户。
1、建立一个异常类,需要继承RuntimeException这个异常类,用来处理业务异常,我设置了,code码,errMsg错误提示,data返回数据,这三个属性,用来处理业务异常,之后为了方便,我特意使用了枚举这种方式用来记录一些code码和errMsg错误提示。
package cn.springboot.yzpt.exception;
import cn.springboot.yzpt.common.CodeEnums;
public class YzptException extends RuntimeException {
//序列化
private static final long serialVersionUID = -3076697639889780533L;
private Integer code;
private String errMsg;
private Object data;
public YzptException(Integer code, String errMsg, Object data) {
super(errMsg);
this.code = code;
this.errMsg = errMsg;
this.data = data;
}
public YzptException(Integer code, String errMsg) {
super(errMsg);
this.code = code;
this.errMsg = errMsg;
}
public YzptException(CodeEnums codeEnums) {
super(codeEnums.getMsg());
this.code = codeEnums.getCode();
this.errMsg = codeEnums.getMsg();
}
public Integer getCode() {
return code;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
附赠:枚举代码
package cn.springboot.yzpt.common;
/**
* 系统枚举,用于业务异常处理
*/
public enum CodeEnums {
SUCCESS(10000, "操作成功"),
SYSTEM_ERR(9999, "系统异常"),
PARA_ERR(8888, "[参数异常]:"),
USER_EXIST(10001, "用户已经注册"),
PWD_CHECK_ERR(10002, "重复密码有误"),
USER_NOT_EXIST(10003, "用户不存在"),
USER_MANY(10004, "用户状态异常,请联系管理员"),
USER_PWD_FAIL(10005, "登录密码错误"),
USER_TOKEN_FAIL(10006, "鉴权失败,请重新登录"),
USER_TOKEN_EMPTY(10007, "获取鉴权信息失败"),
USER_ROLE_LIMIT(10008, "权限不足"),
;
CodeEnums(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
private Integer code;
private String msg;
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;
}
}
2、在server里面进行异常的捕获。
package cn.springboot.yzpt.server.iml.loginUser;
import cn.springboot.yzpt.common.CodeEnums;
import cn.springboot.yzpt.exception.YzptException;
import cn.springboot.yzpt.mapper.loginUser.LoginUserMapper;
import cn.springboot.yzpt.server.loginUser.LoginUserServer;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class LoginUserServerIml implements LoginUserServer {
@Autowired
LoginUserMapper loginUserMapper;
@Override
public boolean login(String user, String password) {
if(StringUtils.isBlank(user)||StringUtils.isBlank(password)){
throw new YzptException(CodeEnums.PARA_ERR);
}
List
3、在controller里面进行try{}catch(){}的异常处理,里面寄存在业务异常处理,也存在系统异常的处理。
package cn.springboot.yzpt.controller.loginUser;
import cn.springboot.yzpt.common.CodeEnums;
import cn.springboot.yzpt.common.EventPublisher;
import cn.springboot.yzpt.common.HttpRequestEntity;
import cn.springboot.yzpt.exception.YzptException;
import cn.springboot.yzpt.server.loginUser.LoginUserServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
@RestController
@RequestMapping("/loginUser")
public class LoginUserController {
private static Logger logger = LoggerFactory.getLogger(LoginUserController.class);
@Autowired
LoginUserServer loginUserServer;
@Autowired
EventPublisher eventPublisher;
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public HttpRequestEntity login(@RequestBody Map userInfo, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
HttpRequestEntity httpRequestEntity = new HttpRequestEntity();
try {
boolean loginType = loginUserServer.login(userInfo.get("user"), userInfo.get("password"));
httpServletRequest.getSession().setAttribute("id", "12314434231");
if (loginType) {
httpRequestEntity.setMessage("登陆成功");
eventPublisher.pushlish(httpServletRequest,httpServletResponse);
logger.info(userInfo.get("user") + "登陆成功");
}
} catch (YzptException e) {
httpRequestEntity.setCode(e.getCode());
httpRequestEntity.setMessage(e.getErrMsg());
} catch (Exception e) {
logger.error("/loginUser/login:出现异常", e);
httpRequestEntity.setCodeEnums(CodeEnums.SYSTEM_ERR);
}
return httpRequestEntity;
}
}
总结:到这基本异常类就搞定了,在server里面进行了异常的捕获,在controller里面一定要有try{}catch(){}的语句,这样代码才不会报错,在这里面,我用到了枚举,当然也可以使用一个类,定义常量来表示。