From a38fd92801be17d5ddebe550334fe348a16200a5 Mon Sep 17 00:00:00 2001 From: heshunme Date: Wed, 3 Jul 2024 14:49:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99HttpMessageNotReadableException?= =?UTF-8?q?=E5=92=8CMissingServletRequestParameterException=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E4=BA=86=E5=85=A8=E5=B1=80=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8=EF=BC=8C=E7=8E=B0=E5=9C=A8=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E5=8F=91=E9=80=81=E7=BB=99=E5=90=8E=E7=AB=AF=E7=9A=84?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=B8=8D=E5=AF=B9=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E4=B9=9F=E8=83=BD=E6=AD=A3=E7=A1=AE=E8=BF=94=E5=9B=9E401?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/Utils/GlobalExceptionHandler.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/org/cmh/backend/Utils/GlobalExceptionHandler.java b/src/main/java/org/cmh/backend/Utils/GlobalExceptionHandler.java index b513f14..e9d8af5 100644 --- a/src/main/java/org/cmh/backend/Utils/GlobalExceptionHandler.java +++ b/src/main/java/org/cmh/backend/Utils/GlobalExceptionHandler.java @@ -2,9 +2,15 @@ package org.cmh.backend.Utils; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + @ControllerAdvice public class GlobalExceptionHandler { @@ -12,4 +18,17 @@ public class GlobalExceptionHandler { public ResponseEntity handleJwtInvalidException(JwtValidationException ex) { return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } + + @ExceptionHandler(MissingServletRequestParameterException.class) + public ResponseEntity> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) { + HashMap response = new HashMap<>(); + response.put("error", ex.getMessage()); + response.put("stackTrace", Arrays.toString(ex.getStackTrace())); + return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + public ResponseEntity handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) { + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); + } } \ No newline at end of file