From 2ce9722b91204f278a371db6c5be637963aa15c4 Mon Sep 17 00:00:00 2001 From: yx9o Date: Sat, 25 Jul 2026 21:56:35 +0800 Subject: [PATCH] fix: handle invalid request parameter types --- .../exception/GlobalExceptionHandler.java | 8 +++++++ .../exception/GlobalExceptionHandlerTest.java | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/server/src/main/java/com/rocketmq/studio/common/exception/GlobalExceptionHandler.java b/server/src/main/java/com/rocketmq/studio/common/exception/GlobalExceptionHandler.java index 79eefd1b..31700aba 100644 --- a/server/src/main/java/com/rocketmq/studio/common/exception/GlobalExceptionHandler.java +++ b/server/src/main/java/com/rocketmq/studio/common/exception/GlobalExceptionHandler.java @@ -27,6 +27,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; @RestControllerAdvice public class GlobalExceptionHandler { @@ -64,6 +65,13 @@ public Result handleHttpMessageNotReadableException(HttpMessageNotReadableExc return Result.error(HttpStatus.BAD_REQUEST.value(), "Invalid request body"); } + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Result handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) { + log.warn("Invalid request parameter: {}", ex.getName()); + return Result.error(HttpStatus.BAD_REQUEST.value(), "Invalid request parameter: " + ex.getName()); + } + @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public Result handleException(Exception ex) { diff --git a/server/src/test/java/com/rocketmq/studio/common/exception/GlobalExceptionHandlerTest.java b/server/src/test/java/com/rocketmq/studio/common/exception/GlobalExceptionHandlerTest.java index 3b5f525c..b746ac0b 100644 --- a/server/src/test/java/com/rocketmq/studio/common/exception/GlobalExceptionHandlerTest.java +++ b/server/src/test/java/com/rocketmq/studio/common/exception/GlobalExceptionHandlerTest.java @@ -23,6 +23,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -56,6 +57,21 @@ void preservesBadRequestBusinessStatusAndEnvelope() throws Exception { .andExpect(jsonPath("$.message").value("failure-400")); } + @Test + void returnsBadRequestWhenRequestParamTypeIsInvalid() throws Exception { + mockMvc.perform(get("/test/number").param("startTime", "invalid")) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.code").value(400)) + .andExpect(jsonPath("$.message").value("Invalid request parameter: startTime")); + } + + @Test + void acceptsValidRequestParamType() throws Exception { + mockMvc.perform(get("/test/number").param("startTime", "1000")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data").value(1000)); + } + @RestController static class FailingController { @@ -63,5 +79,10 @@ static class FailingController { Result fail(@PathVariable int code) { throw new BusinessException(code, "failure-" + code); } + + @GetMapping("/test/number") + Result number(@RequestParam Long startTime) { + return Result.ok(startTime); + } } }