This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new c7a114a1b fix(api): preserve the Allow header on method-not-allowed 
responses (#6888)
c7a114a1b is described below

commit c7a114a1b9f498c4e429cf0f9cba41c50f9ee227
Author: 健康宝 <[email protected]>
AuthorDate: Fri Nov 27 11:55:25 2026 +0800

    fix(api): preserve the Allow header on method-not-allowed responses (#6888)
---
 .../common/exception/GlobalExceptionHandler.java      | 19 +++++++++++++++----
 .../common/exception/GlobalExceptionHandlerTest.java  |  9 +++++++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git 
a/server/src/main/java/org/apache/rocketmq/studio/common/exception/GlobalExceptionHandler.java
 
b/server/src/main/java/org/apache/rocketmq/studio/common/exception/GlobalExceptionHandler.java
index 8134d7bca..7221e7706 100644
--- 
a/server/src/main/java/org/apache/rocketmq/studio/common/exception/GlobalExceptionHandler.java
+++ 
b/server/src/main/java/org/apache/rocketmq/studio/common/exception/GlobalExceptionHandler.java
@@ -20,6 +20,8 @@ import jakarta.validation.ConstraintViolationException;
 import org.apache.rocketmq.studio.cluster.metrics.PrometheusException;
 import org.apache.rocketmq.studio.common.domain.Result;
 import org.apache.rocketmq.studio.ops.ai.LlmGatewayException;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.converter.HttpMessageNotReadableException;
@@ -35,6 +37,8 @@ import 
org.springframework.web.method.annotation.MethodArgumentTypeMismatchExcep
 import org.springframework.web.servlet.resource.NoResourceFoundException;
 import lombok.extern.slf4j.Slf4j;
 
+import java.util.Set;
+
 @Slf4j
 @RestControllerAdvice
 public class GlobalExceptionHandler {
@@ -126,14 +130,21 @@ public class GlobalExceptionHandler {
     }
 
     /**
-     * Requests with an unsupported HTTP method must be reported as 405, not 
500.
+     * Requests with an unsupported HTTP method must be reported as 405, not 
500, and the
+     * response must keep the Allow header Spring synthesizes for this 
exception.
      */
     @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
-    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
-    public Result<?> handleHttpRequestMethodNotSupportedException(
+    public ResponseEntity<Result<?>> 
handleHttpRequestMethodNotSupportedException(
             HttpRequestMethodNotSupportedException ex) {
         log.warn("Method not allowed: {}", ex.getMessage());
-        return Result.error(HttpStatus.METHOD_NOT_ALLOWED.value(), 
ex.getMessage());
+        HttpHeaders headers = new HttpHeaders();
+        Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
+        if (supportedMethods != null && !supportedMethods.isEmpty()) {
+            headers.setAllow(supportedMethods);
+        }
+        return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED)
+                .headers(headers)
+                .body(Result.error(HttpStatus.METHOD_NOT_ALLOWED.value(), 
ex.getMessage()));
     }
 
     /**
diff --git 
a/server/src/test/java/org/apache/rocketmq/studio/common/exception/GlobalExceptionHandlerTest.java
 
b/server/src/test/java/org/apache/rocketmq/studio/common/exception/GlobalExceptionHandlerTest.java
index 583434710..a91e45aa4 100644
--- 
a/server/src/test/java/org/apache/rocketmq/studio/common/exception/GlobalExceptionHandlerTest.java
+++ 
b/server/src/test/java/org/apache/rocketmq/studio/common/exception/GlobalExceptionHandlerTest.java
@@ -92,6 +92,15 @@ class GlobalExceptionHandlerTest {
                 .andExpect(jsonPath("$.code").value(405));
     }
 
+    @Test
+    void methodNotAllowedKeepsTheAllowHeader() throws Exception {
+        
mockMvc.perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders
+                        .post("/test/business/400"))
+                .andExpect(status().isMethodNotAllowed())
+                
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers
+                        
.header().string(org.springframework.http.HttpHeaders.ALLOW, "GET"));
+    }
+
     @Test
     void unacceptableResponseMediaTypeReturns406WithEnvelopeInsteadOf500() 
throws Exception {
         mockMvc.perform(get("/test/not-acceptable"))

Reply via email to