From 6ebddcc7dc5b58162fc59cac304691f049ed5a5d Mon Sep 17 00:00:00 2001 From: liuhy Date: Fri, 24 Jul 2026 09:11:16 -0700 Subject: [PATCH] Handle missing request parameters --- .../common/exception/GlobalExceptionHandler.java | 8 ++++++++ .../exception/GlobalExceptionHandlerTest.java | 14 ++++++++++++++ 2 files changed, 22 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..53b8d123 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 @@ -24,6 +24,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -64,6 +65,13 @@ public Result handleHttpMessageNotReadableException(HttpMessageNotReadableExc return Result.error(HttpStatus.BAD_REQUEST.value(), "Invalid request body"); } + @ExceptionHandler(MissingServletRequestParameterException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) { + String message = ex.getParameterName() + " is required"; + return Result.error(HttpStatus.BAD_REQUEST.value(), message); + } + @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..384b9777 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,14 @@ void preservesBadRequestBusinessStatusAndEnvelope() throws Exception { .andExpect(jsonPath("$.message").value("failure-400")); } + @Test + void returnsBadRequestWhenRequiredRequestParamIsMissing() throws Exception { + mockMvc.perform(get("/test/required-param")) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.code").value(400)) + .andExpect(jsonPath("$.message").value("key is required")); + } + @RestController static class FailingController { @@ -63,5 +72,10 @@ static class FailingController { Result fail(@PathVariable int code) { throw new BusinessException(code, "failure-" + code); } + + @GetMapping("/test/required-param") + Result requiredParam(@RequestParam String key) { + return Result.ok(key); + } } }