如果确保代码没有任何问题,检查下 下列几项:
- 方法上增加需要捕获的HTTP状态码 @ResponseStatus(HttpStatus.BAD_REQUEST)
/**
* 参数解析失败异常
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ResponseMsg methodHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.error("参数解析失败", e);
return new ResponseMsg(ErrorCode.PARAM_FAIL);
}
- 配置没有被扫描到,解决办法:配置文件需要开启:spring.mvc.throw-exception-if-no-handler-found: true 设置
spring:
mvc:
throw-exception-if-no-handler-found: true
- 全局捕获异常处理类没有被扫描到,全局异常404异常默认是不会拦截,需要在yml文件中加入以下配置,解决办法:启动类增加 @SpringBootApplication(scanBasePackages = {“com.yang”})
@SpringBootApplication(scanBasePackages = {"com.yang"})
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class , args);
}
}