@ResponseStatusSpringMVC
@ResponseStatus注解作用
一:作用
简单介绍:自定义给前端返回状态代码与错误信息message
@ResponseStatus注解有两种用法,一种是加载自定义异常类上,一种是加在目标方法中 这里我们说一下加在目标方法上的这种情况,注解中有两个参数,value属性设置异常的状态码,reaseon是异常的描述message,
@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户名和密码不匹配!")
二:使用
一般可以在一个@RestControllerAdvice全局异常处理类的异常处理方法中设置
@ResponseStatus(value =HttpStatus.OK)
//要捕获的异常类
@ExceptionHandler(MaxUploadSizeExceededException.class)
@ResponseBody
public JsonResultUtil handleMaxUploadSizeExceededException(HttpServletResponse httpServletResponse, MaxUploadSizeExceededException e) throws IOException {
logger.info(ConstantUtil.UPLOAD_SIZE_EXCEEDS);
//自定义Vo类JsonResultUtil
return new JsonResultUtil(ConstantUtil.UPLOAD_SIZE_EXCEEDS_NUM,ConstantUtil.UPLOAD_SIZE_EXCEEDS+ fileZine);
}
🚀资料学习与引用
