java - Spring mvc的异常处理:IllegalArgumentException

 守望天空2502870383 发布于 2022-10-29 14:05

1.业务描述

我试图利用Spring MVC捕获处理URL参数错误的异常。

(1)通常情况,请求URL如下:

.../channel/beforeAdd?openid=1000

之后我们利用openid在数据库中进行匹配用户。

(2)异常发生,请求URL如下:

.../channel/before

(3)Spring MVC的异常处理报错如下:

2016-04-27 18:22:06.140 ERROR 5800 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!] with root cause

(4)处理URL的控制器如下:

@Controller
@RequestMapping(value = "/channel")
public class ChannelController {
    @Autowired
    private CardRepository cardRepository;
    @RequestMapping(value = "/beforeAddChannel")
    public ModelAndView beforeAddChannel(String openid) throws CardNotFoundException {
        Card card = cardRepository.findOne(openid);
        if(card == null) {
            throw new CardNotFoundException();
        }
        ModelAndView modelAndView = new ModelAndView("addChannel");
        modelAndView.addObject("card", card);
        return modelAndView;
    }
}

2.自定义异常处理

注意到两个异常分别为:InvalidDataAccessApiUsageException和IllegalArgumentException。

我们利用@ExceptionHandler处理IllegalArgumentException

@ExceptionHandler(IllegalArgumentException.class)
public String handleIllegalArgumentException(){
    return "errors/illegalArgumentException";
}

网页没有跳转illegalArgumentException.html,系统报错依然。

我们再处理InvalidDataAccessApiUsageException

@ExceptionHandler(InvalidDataAccessApiUsageException.class)
public String handleInvalidDataAccessApiUsageException(){
    return "errors/invalidDataAccessApiUsage";
}

网页跳转invalidDataAccessApiUsage.html,系统不再报错。

3.问题描述

参数检查不是应该再数据库事务检查之前吗?参数错误不是应该程序中断吗?为什么这里我们自定义的参数的异常捕获无效,数据库事务的异常捕获却有效?

2 个回答
  • 正如@onelove 所说,非法参数异常IllegalArgumentException不是用来检测控制器参数是否空值。我写了一个测试:

    @Controller
    @ResponseBody
    public class HelloController {
        @RequestMapping(value = "")
        String index(String id){
            System.out.println(id);
            return id;
        }
    }
    

    请求URL如下:http://localhost:8080
    控制台返回null

    Spring MVC的控制器来说,如果在请求中找不到控制器参数,参数就会置空,而不会抛出异常
    这里的IllegalArgumentException异常之所以会产生是由于JpaRepository类中的findOne方法不允许参数为空。

    2022-10-31 00:17 回答
  • 参数 openId为null,然后直接 orm 报参数不能为空,应该是的,不太确定。

    2022-10-31 00:17 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有