可以使用HttpServletResponse.sendError(int)方法更改http响应状态代码,例如
@ExceptionHandlervoid handleIllegalArgumentException(IllegalArgumentException e, HttpServletResponse response) throws IOException { response.sendError(HttpStatus.BAD_REQUEST.value());}另外,
@ExceptionHandler如果您有两个或多个异常来生成相同的响应状态,则可以在批注中声明异常类型:
@ExceptionHandler({IllegalArgumentException.class, NullPointerException.class})void handleBadRequests(HttpServletResponse response) throws IOException { response.sendError(HttpStatus.BAD_REQUEST.value());}可以在我的博客文章中找到更多信息。



