From 68ff33beff11310e830b0615b5a81f83fad0f683 Mon Sep 17 00:00:00 2001
From: yxh <172933527@qq.com>
Date: 星期日, 08 十一月 2026 23:33:14 +0800
Subject: [PATCH] ai优化

---
 jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/web/core/handler/GlobalExceptionHandler.java |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/web/core/handler/GlobalExceptionHandler.java b/jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/web/core/handler/GlobalExceptionHandler.java
index 009c5ee..9ae60a9 100644
--- a/jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/web/core/handler/GlobalExceptionHandler.java
+++ b/jh-framework/jh-spring-boot-starter-web/src/main/java/cn/lihu/jh/framework/web/core/handler/GlobalExceptionHandler.java
@@ -4,6 +4,8 @@
 import cn.hutool.core.map.MapUtil;
 import cn.hutool.core.util.ObjUtil;
 import cn.hutool.core.util.StrUtil;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
 import cn.lihu.jh.framework.apilog.core.service.ApiErrorLogFrameworkService;
 import cn.lihu.jh.framework.common.exception.ServiceException;
 import cn.lihu.jh.framework.common.exception.util.ServiceExceptionUtil;
@@ -17,6 +19,7 @@
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.springframework.http.converter.HttpMessageNotReadableException;
 import org.springframework.security.access.AccessDeniedException;
 import org.springframework.util.Assert;
 import org.springframework.validation.BindException;
@@ -124,6 +127,46 @@
     }
 
     /**
+     * 澶勭悊璇锋眰浣擄紙@RequestBody锛夊弽搴忓垪鍖栧け璐ャ��
+     *
+     * <h3>涓轰粈涔堥渶瑕佸崟鐙鐞�</h3>
+     * 涔嬪墠娌℃湁杩欎釜澶勭悊鍣紝JSON 閲岀殑瀛楁绫诲瀷涓嶅锛堜緥濡� {@code "bookCheckType": "甯歌蹇冪數鍥�"}锛�
+     * 鑰屽疄浣撳瓧娈垫槸 {@code Integer}锛夋椂浼氭姏 {@link HttpMessageNotReadableException}锛�
+     * 涓�璺惤鍒� {@link #defaultExceptionHandler} 鍏滃簳 鈥斺�� 鍓嶇鍙湅鍒� <b>500銆岀郴缁熷紓甯搞��</b>锛�
+     * 瀹屽叏鐪嬩笉鍑烘槸鍙傛暟鏍煎紡闂锛堝疄娴� 2026-10-28 鎺掓煡 {@code /ecg/appointment/create} 鍗充负姝わ級銆�
+     *
+     * <p>鐜板湪鏀逛负杩斿洖 <b>400</b> 骞舵槑纭寚鍑恒�屽摢涓瓧娈点�佹敹鍒颁粈涔堝�笺�佹湡鏈涗粈涔堢被鍨嬨�嶃��
+     */
+    @ExceptionHandler(HttpMessageNotReadableException.class)
+    public CommonResult<?> httpMessageNotReadableExceptionHandler(HttpMessageNotReadableException ex) {
+        log.warn("[httpMessageNotReadableExceptionHandler]", ex);
+        return CommonResult.error(BAD_REQUEST.getCode(), "璇锋眰鍙傛暟鏍煎紡閿欒:" + buildMessageNotReadableDetail(ex));
+    }
+
+    /**
+     * 鎶婂弽搴忓垪鍖栧紓甯哥炕璇戞垚浜鸿兘鐪嬫噦鐨勮鏄庯細瀛楁鍚� + 鏀跺埌鐨勫�� + 鏈熸湜鐨勭被鍨嬨��
+     */
+    private String buildMessageNotReadableDetail(HttpMessageNotReadableException ex) {
+        Throwable cause = ex.getMostSpecificCause();
+        // 鈶� 瀛楁绫诲瀷涓嶅尮閰嶏紙鏈�甯歌锛夛細鎷垮埌浜嗗瓧娈靛悕 / 瀹為檯鍊� / 鏈熸湜绫诲瀷
+        if (cause instanceof InvalidFormatException) {
+            InvalidFormatException ife = (InvalidFormatException) cause;
+            String field = ife.getPath().isEmpty()
+                    ? "" : ife.getPath().get(ife.getPath().size() - 1).getFieldName();
+            Object value = ife.getValue();
+            Class<?> target = ife.getTargetType();
+            return String.format("瀛楁銆�%s銆嶇被鍨嬩笉姝g‘锛堟敹鍒板��:%s锛屾湡鏈涚被鍨�:%s锛�",
+                    StrUtil.blankToDefault(field, "鏈煡瀛楁"), value, null == target ? "鏈煡" : target.getSimpleName());
+        }
+        // 鈶� 璇锋眰浣撶己澶� / JSON 璇硶閿欒绛夛細鐢ㄦ渶鍏蜂綋鐨勫師鍥�
+        if (cause instanceof JsonParseException) {
+            return "JSON 鏍煎紡涓嶆纭�:" + ((JsonParseException) cause).getOriginalMessage();
+        }
+        String message = null == cause ? ex.getMessage() : cause.getMessage();
+        return StrUtil.blankToDefault(message, "璇锋眰浣撴棤娉曡В鏋�");
+    }
+
+    /**
      * 澶勭悊 SpringMVC 鍙傛暟鏍¢獙涓嶆纭�
      */
     @ExceptionHandler(MethodArgumentNotValidException.class)

--
Gitblit v1.9.3