如何使用SpringMVC errorHandler处理有效的jsr303以返回自定义异常?

时间:2018-12-26 09:31:03

标签: spring-mvc

当我在@RequestParam上使用jsr303时:

@RequestParam("page") @Min(value = 1) int page

使用无效参数时,它总是会向我发送如下消息:

{
    "timestamp": "2018-12-26T08:43:19.798+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "getUserInfo.page: must be greater than or equal to 1",
    "path": "/users"
}

但是我想通过使用ControllerAdvice来响应我的自定义异常

@ControllerAdvice
public class ExceptionAdvice {

@ExceptionHandler({ JSR-303Exception.class })
@ResponseBody
public String handleArrayIndexOutOfBoundsException(Exception e) {
    e.printStackTrace();
    return "testArrayIndexOutOfBoundsException";
}

并做出如下响应:

{
    "errMsg": "page must lager than 0"
}

但是我不知道@min抛出什么异常类。

是解决我的问题的正确方法还是有更好的解决方案?

0 个答案:

没有答案