超级悠悠
高性能程序定制、UI定制
542165

访问

0

评论

19

动态

2026

运行

来过
设计群: 70888820
位置: luzhou·sichuan

- 今日签到 -

今日暂无签到

首页破碎代码SpringBoot3拦截@PathVariable注解异常

SpringBoot3拦截@PathVariable注解异常

评论 0/访问 1033/分类: 破碎代码/发布时间:
/**
 * 文章详情
 *
 * @param id 文章ID
 */
@GetMapping("/detail/{id}")
public R<ArticleVo> detail(@PathVariable("id") Long id) {
    return R.ok(articleService.detail(id));
}

可以看到以上我的代码,需要一个Long类型的id

如果参数类型不对,要怎么拦截呢?

方法来了,如下!

/**
 * 接口方法参数类型异常
 */
@ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
@ResponseBody
public Object methodArgumentTypeMismatchException(Throwable e) {
    log.error("url参数异常,请检查参数类型是否匹配!", e);
    return R.error("请检查参数类型是否正确");
}

收藏

点赞

打赏