拦截由@validated 报出的异常MethodArgumentNotValidException并

public class WordsRequest implements Serializable {

    private static final long serialVersionUID = 4048730427527061626L;
    @ApiModelProperty("昵称")
    private String nickname;

    @ApiModelProperty("留言内容")
    @Size(max = 30,min = 1,message = "留言内容过长")
    private String msgContent;

    @ApiModelProperty("消息类型 0 img  1语音  2文字")
    private Integer msgType;

}
 
ResponseEntity<BaseOutput<WordsDTO>> sendWords(
                                                     @Valid @RequestBody WordsRequest  wordsRequest ){
}



我们使用的版本是springboot 2.0.4.RELEASE

我们使用@Valid 验证WordsRequest 入参合法性,但是当msgContent内容长度超过限制30个字符。将响应客户端 

http code 400  Bad Request,并且没有任何错误提示。这样的体验非常得不友好。后端也没有任何日志输出。日志级别是INFO。

但是我们也已经统一拦截所有异常信息,那为啥没有日志打印并且响应请求也是400呢,代码如下。

    @ExceptionHandler(value = {Throwable.class})
    @ResponseBody
    public ResponseEntity<?> constraintViolationHandler(HttpServletRequest req, Throwable exception) {
}

当然我们想看到具体错误原因,才知道情况。 调整日志级别为DEBUG.看到打印日志信息,如下。

org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument at index 3 in method: org.springframework.http.ResponseEntity<crplatform.crp.domain.dto.BaseOutput<com.xuehai.crplatform.crp.domain.dto.wordsgroup.WordsDTO>> crplatform.crp.interfaces.controller.WordsGroupController.sendWords(java.lang.Long,java.lang.String,java.lang.String,crplatform.crp.domain.dto.wordsgroup.WordsRequest), with 1 error(s): [Field error in object 'wordsRequest' on field 'msgContent': rejected value [hello rgregregregregregregregrehh]; codes [Size.wordsRequest.msgContent,Size.msgContent,Size.java.lang.String,Size];

发现具体异常是MethodArgumentNotValidException,这个类。我得到最简单想法是先拦截这个异常类。

    @ExceptionHandler(value = {Throwable.class,MethodArgumentNotValidException.class})

重试部署试一试,但重启异常错误,错误如此下:

Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]

懵逼了!于是找度娘找到一个答案。https://yq.aliyun.com/articles/398366 

重写父类ResponseEntityExceptionHandler 的方法handleMethodArgumentNotValid

else if (ex instanceof MethodArgumentNotValidException) {
            //这里配置响应请求码400
			HttpStatus status = HttpStatus.BAD_REQUEST;
			return handleMethodArgumentNotValid((MethodArgumentNotValidException) ex, headers, status, request);
		}

/**
	 * Customize the response for MethodArgumentNotValidException.
	 * <p>This method delegates to {@link #handleExceptionInternal}.
	 * @param ex the exception
	 * @param headers the headers to be written to the response
	 * @param status the selected response status
	 * @param request the current request
	 * @return a {@code ResponseEntity} instance
	 */
	protected ResponseEntity<Object> handleMethodArgumentNotValid(
			MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        //响应体是null,没有数据。
        //我们要做得就是重写这个方法。    
		return handleExceptionInternal(ex, null, headers, status, request);
	}

重新部署好了。能看到我们自定义的异常信息了-留言信息过长 。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jiguansheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值