2018-12-11 全局异常处理器

2018-12-11  本文已影响0人  培根好吃

1.全局异常处理器

1.1 在codeMsg里增加 一个参数拼接方法 filledArgs

public static CodeMsg BIND_ERROR=new CodeMsg(500101,"参数校验异常:%s"); //有个拼接异常%s
public CodeMsg fillArgs(Object...args) {
        int code=this.code;
        String message=String.format(this.msg, args);
        return new CodeMsg(code,message);
    }

1.2 全局异常处理类

package com.ryan.miaosha.exception;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ryan.miaosha.domain.CodeMsg;
import com.ryan.miaosha.domain.Result;


@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler(value=Exception.class)
    public Result<String> exceptionHandler(HttpServletRequest request, Exception e){
        e.printStackTrace();
        if(e instanceof GlobalException) {
            GlobalException ex = (GlobalException)e;
            return Result.error(ex.getCm());
        }else if(e instanceof BindException) {
            BindException ex = (BindException)e;
            List<ObjectError> errors = ex.getAllErrors();
            ObjectError error = errors.get(0);
            String msg = error.getDefaultMessage();
            return Result.error(CodeMsg.BIND_ERROR.filledArgs(msg));
        }else {
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }
}


1.3 全局异常类

package com.ryan.miaosha.exception;

import com.ryan.miaosha.domain.CodeMsg;

public class GlobalException extends RuntimeException{

    private static final long serialVersionUID = 1L;
    
    private CodeMsg cm;
    
    public GlobalException(CodeMsg cm) {
        super(cm.toString());
        this.cm = cm;
    }

    public CodeMsg getCm() {
        return cm;
    }

}

1.4 全局异常类

@Service
public class MiaoshaUserService {
    
    
    //public static final String COOKI_NAME_TOKEN = "token";
    
    @Autowired
    MiaoshaUserDao miaoshaUserDao;
    
    @Autowired
    RedisService redisService;
    
    public MiaoshaUser getById(long id) {
        return miaoshaUserDao.getById(id);
    }
    
    public boolean login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        long id=Long.parseLong(mobile);
        MiaoshaUser userById = getById(id);
        if(userById==null) {
             throw new GlobalException(CodeMsg.USER_NOT_EXIST);
        }
        String saltDB = userById.getSalt();
        String passWEB=loginVo.getPassword();
        String passValidated=MD5Util.WebPassToDB(passWEB, saltDB);
        String passDB=userById.getPassword();
        if(!passDB.equals(passValidated)) {
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }
        return true;
    }

1.5 controller的内容变得很简洁



//请求成功    为什么传入loginVo  因为要接收网页表单传来的参数  将手机号和密码传入一个对象中 
    @RequestMapping("/do_login")
    @ResponseBody
    Result<Boolean> dologin(@Valid LoginVo loginVo) {
        /*String mobile = loginVo.getMobile();
        String passInput=loginVo.getPassword();
        if(StringUtils.isEmpty(mobile)) {
            return Result.error(CodeMsg.MOBILE_EMPTY);
        }
        if(!ValidatorUtil.isMobile(mobile)) {
            return Result.error(CodeMsg.MOBILE_ERROR);
        }
        if(StringUtils.isEmpty(passInput)) {
            return Result.error(CodeMsg.PASSWORD_EMPTY);
        }
         log.info(loginVo.toString());*/
         //登录
        /* CodeMsg cm = miaoshaUserService.login(loginVo);
         if(cm.getCode()==0) {
             return Result.success(true);
         }else {
             return Result.error(cm);
         }*/
        miaoshaUserService.login(loginVo);
        return Result.success(true);
    }
上一篇下一篇

猜你喜欢

热点阅读