开发者社区> 问答> 正文

关于Spring的@ExceptionHandler不执行的问题-java报错

"

今天敲了会代码,想用@ExceptionHandler处理异常试试。

写好一段程序后,抛异常怎么也走不进@ExceptionHandler修饰的方法,大家帮忙看看。

package cn.net.bysoft.blackpearl.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import cn.net.bysoft.blackpearl.entity.ResponseInfo;
import cn.net.bysoft.blackpearl.entity.User;
import cn.net.bysoft.blackpearl.exception.UserLockedException;
import cn.net.bysoft.blackpearl.exception.UserLoginFailException;
import cn.net.bysoft.blackpearl.service.UserService;

@RestController
@RequestMapping(value = { "/controller/users" })
public class UserController {

    @Autowired
    private UserService userService;

    @ExceptionHandler(UserLockedException.class)
    @ResponseStatus(HttpStatus.LOCKED)
    public ResponseInfo handlerUserLocked(UserLockedException userLockedException) {
        return new ResponseInfo(HttpStatus.LOCKED, userLockedException.getErrMsg());
    }

    @ExceptionHandler(UserLoginFailException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResponseInfo handlerUserLoginFail(UserLockedException userLockedException) {
        System.out.println("123");
        return new ResponseInfo(HttpStatus.NOT_FOUND, userLockedException.getErrMsg());
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseInfo login(@RequestBody User user, HttpSession httpSession) {
        // 通过帐号和密码查询用户
        User currentUser = userService.findByEmailAndPassword(user);

        // 判断是否查询到了用户
        if (currentUser != null) {
            // 将用户设置在Session中
            httpSession.setAttribute("User", currentUser);
            // 判断用户是否被锁定
            if (!currentUser.isLocked()) {
                throw new UserLockedException("UserLocked", "您登录的帐号已被锁定。");
            }
        } else {
            // 登录失败
            throw new UserLoginFailException("UserNotFound", "帐号或密码不存在。");
        }
        // 登陆成功
        return new ResponseInfo(HttpStatus.OK);
    }

    @RequestMapping(value = "/current", method = RequestMethod.GET)
    public ResponseInfo getCurrentUser(HttpSession httpSession) {
        // 从Session中获得当前用户
        return new ResponseInfo(HttpStatus.OK, httpSession.getAttribute("User"));
    }

}

"

展开
收起
montos 2020-06-04 13:22:50 916 0
1 条回答
写回答
取消 提交回答
  • " @ExceptionHandler(UserLoginFailException.class)
        @ResponseStatus(HttpStatus.NOT_FOUND)
        public ResponseInfo handlerUserLoginFail(UserLockedException userLockedException) {
            System.out.println("123");
            return new ResponseInfo(HttpStatus.NOT_FOUND, userLockedException.getErrMsg());

        }

    参数类型写错了,弄死我吧!再也不复制粘贴了!

    "
    2020-06-04 13:25:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载