springmvc请求参数异常统⼀处理 1、ExceptionHandlerController
troller;
MessageFormat;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.verter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import com.alibaba.fastjson.JSONObject;
eption.ForbiddenException;
utils.UtilFunctions;
@ControllerAdvice
public class ExceptionHandlerController {
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public JSONObject runtimeExceptionHandler(RuntimeException ex) {
("runtimeExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
JSONObject response = new JSONObject();
response.put("message", "Internal Server Error");
return response;
}
@ExceptionHandler(NullPointerException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public JSONObject nullPointerExceptionHandler(NullPointerException ex) {
("nullPointerExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
JSONObject response = new JSONObject();
response.put("message", "Internal Server Error");
return response;
}
/*----- REQUEST ERROR -----*/
@ExceptionHandler({ ForbiddenException.class })
@ResponseStatus(HttpStatus.FORBIDDEN)
@ResponseBody
public JSONObject requestForbidden(ForbiddenException ex) {
("ForbiddenExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
JSONObject response = new JSONObject();
response.put("message", ex.getMessage());
return response;
}
@ExceptionHandler({ TypeMismatchException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public JSONObject requestTypeMismatch(TypeMismatchException ex) {
("TypeMismatchExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
JSONObject response = new JSONObject();
// response.put("message", "Bad Request");
// response.put("message", "Bad Request, parameter type of " + ex.getPropertyName() + " need be " + ex.getRequiredType());
if (Double.class.RequiredType()) || Integer.class.RequiredType())) {
response.put("message", "Bad Request, " + ex.getValue() + " not a number");
} else {
String strTemplate = "Bad Request, {0} is invalid, a type of {1} is needed";
response.put("message", MessageFormat.format(strTemplate, ex.getValue(), ex.getRequiredType().getName()));
}
return response;
}
@ExceptionHandler({ MissingServletRequestParameterException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public JSONObject requestMissingServletRequest(MissingServletRequestParameterException ex) {
("MissingServletRequestParameterExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
JSONObject response = new JSONObject();
// response.put("message", "Bad Request");
String strTemplate = "Bad Request, param:{0} is required, type:{1}";
response.put("message", MessageFormat.format(strTemplate, ex.getParameterName(), ex.getParameterType()));
return response;
}
@ExceptionHandler({ NoSuchRequestHandlingMethodException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public JSONObject NoSuchRequestHandlingMethodExceptionHandler(NoSuchRequestHandlingMethodException ex) { ("NoSuchRequestHandlingMethodExceptionHandler, msg:{}, exception:{}", ex.toString(), ex); JSONObject response = new JSONObject();
response.put("message", "Not Found");
return response;
}
/*----- REQUEST ERROR -----*/
@ExceptionHandler({ HttpMessageNotReadableException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public JSONObject requestNotReadable(HttpMessageNotReadableException ex) {
("HttpMessageNotReadableExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
JSONObject response = new JSONObject();
response.put("message", "Bad Request");
return response;
}
@ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ResponseBody
public JSONObject request405(HttpRequestMethodNotSupportedException ex) {
("HttpRequestMethodNotSupportedExceptionHandler, msg:{}, exception:{}", ex.toString(), ex); JSONObject response = new JSONObject();
// response.put("message", "Method Not Allowed");
response.put("message", ex.getMessage());
return response;
}
}
2、postman测试
3、异常增强类型:
NullPointerException,RunTimeException,ClassCastException,
NoSuchMethodException,IOException,IndexOutOfBoundsException
以及springmvc⾃定义异常等,如下:
SpringMVC⾃定义异常对应的status code
Exception HTTP Status Code
ConversionNotSupportedException 500 (Internal Server Error)
HttpMessageNotWritableException 500 (Internal Server Error)
HttpMediaTypeNotSupportedException 415 (Unsupported Media Type) HttpMediaTypeNotAcceptableException 406 (Not Acceptable) HttpRequestMethodNotSupportedException 405 (Method Not Allowed) NoSuchRequestHandlingMethodException 404 (Not Found)
TypeMismatchException 400 (Bad Request)
HttpMessageNotReadableException 400 (Bad Request)nullpointerexception为什么异常
MissingServletRequestParameterException 400 (Bad Request)
参考资料:
(1)
(2)
(3)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论