Springboot中HttpMessageNotReadableException异常处理冲突求助
解决Spring Boot异常处理映射冲突问题
冲突原因
你的异常处理类继承了ResponseEntityExceptionHandler,父类中已存在handleException(Exception, WebRequest)方法;而你自定义的handleException(Exception)方法在匹配HttpMessageNotReadableException时,Spring无法判断应调用哪个方法,从而触发映射歧义。
解决方案
方案一:修改自定义方法参数为具体异常类
将方法参数从泛型的Exception改为目标异常类HttpMessageNotReadableException,让匹配逻辑更精确,避免和父类方法冲突:
package com.proj.my.advice; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import lombok.extern.slf4j.Slf4j; @RestControllerAdvice @Slf4j public class ApplicationExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(HttpMessageNotReadableException.class) public ResponseEntity<String> handleHttpMessageNotReadable(HttpMessageNotReadableException e){ log.error("请求参数解析失败:{}", e.getMessage()); return new ResponseEntity<>("价格参数格式错误,请输入有效的数字", HttpStatus.BAD_REQUEST); } }
方案二:重写父类的专属异常处理方法
ResponseEntityExceptionHandler本身提供了处理HttpMessageNotReadableException的专用方法handleHttpMessageNotReadable,直接重写该方法即可,无需额外添加@ExceptionHandler注解:
package com.proj.my.advice; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import lombok.extern.slf4j.Slf4j; @RestControllerAdvice @Slf4j public class ApplicationExceptionHandler extends ResponseEntityExceptionHandler { @Override protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) { log.error("请求参数解析失败:{}", ex.getMessage()); return new ResponseEntity<>("价格参数格式错误,请输入有效的数字", HttpStatus.BAD_REQUEST); } }
方案三:移除父类继承关系
如果不需要父类提供的其他默认异常处理逻辑,可以直接去掉ResponseEntityExceptionHandler继承,仅保留自定义异常处理:
package com.proj.my.advice; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import lombok.extern.slf4j.Slf4j; @RestControllerAdvice @Slf4j public class ApplicationExceptionHandler { @ExceptionHandler(HttpMessageNotReadableException.class) public ResponseEntity<String> handleHttpMessageNotReadable(HttpMessageNotReadableException e){ log.error("请求参数解析失败:{}", e.getMessage()); return new ResponseEntity<>("价格参数格式错误,请输入有效的数字", HttpStatus.BAD_REQUEST); } }
内容的提问来源于stack exchange,提问作者Dadinho2000
相关产品推荐
相关产品推荐

