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

davsclaus pushed a commit to branch camel-4.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.18.x by this push:
     new 9c1aa8244f66 [4.18.x] CAMEL-24594: Log REST DSL response marshalling 
failures (#27274)
9c1aa8244f66 is described below

commit 9c1aa8244f6623d43aa71576cab6929f852ed5b5
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Oct 2 14:23:02 2026 +0200

    [4.18.x] CAMEL-24594: Log REST DSL response marshalling failures (#27274)
    
    Co-Authored-By: Claude Opus 4.8 <[email protected]>
---
 .../vertx/VertxRestBindingMarshalFailureTest.java  | 38 ++++++++++++++++++++++
 .../camel/support/processor/RestBindingAdvice.java | 10 ++++++
 2 files changed, 48 insertions(+)

diff --git 
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxRestBindingMarshalFailureTest.java
 
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxRestBindingMarshalFailureTest.java
index c9c757e06f27..922b0fba59cd 100644
--- 
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxRestBindingMarshalFailureTest.java
+++ 
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxRestBindingMarshalFailureTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.platform.http.vertx;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.model.rest.RestBindingMode;
 import org.junit.jupiter.api.Test;
@@ -108,6 +109,43 @@ public class VertxRestBindingMarshalFailureTest {
         }
     }
 
+    @Test
+    public void testExplicitResponseCodeStillWinsOnMarshalFailure() throws 
Exception {
+        final CamelContext context = 
VertxPlatformHttpEngineTest.createCamelContext();
+
+        try {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() {
+                    restConfiguration().bindingMode(RestBindingMode.json);
+
+                    rest("/demo")
+                            .get("/{id}").to("direct:demo");
+
+                    from("direct:demo")
+                            // an explicitly set response code must still win, 
even though the response marshalling
+                            // fails afterwards (the failure is logged 
server-side, but does not override the code)
+                            .process(e -> {
+                                
e.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
+                                e.getMessage().setBody(new BadPojo());
+                            });
+                }
+            });
+
+            context.start();
+
+            given()
+                    .when()
+                    .get("/demo/42")
+                    .then()
+                    // the explicitly set 200 wins over the marshal failure, 
consistent with plain routes
+                    .statusCode(200)
+                    .body(emptyString());
+        } finally {
+            context.stop();
+        }
+    }
+
     /**
      * A POJO that always fails to marshal because its getter throws.
      */
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
index 4b9006747968..62116f707d2f 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
@@ -38,6 +38,8 @@ import org.apache.camel.support.MessageHelper;
 import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Used for Rest DSL with binding to json/xml for incoming requests and 
outgoing responses.
@@ -51,6 +53,8 @@ import org.apache.camel.util.ObjectHelper;
  */
 public class RestBindingAdvice extends ServiceSupport implements 
CamelInternalProcessorAdvice<Map<String, Object>> {
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(RestBindingAdvice.class);
+
     private static final String STATE_KEY_DO_MARSHAL = "doMarshal";
     private static final String STATE_KEY_ACCEPT = "accept";
     private static final String STATE_JSON = "json";
@@ -465,6 +469,12 @@ public class RestBindingAdvice extends ServiceSupport 
implements CamelInternalPr
                 }
             }
         } catch (Exception e) {
+            // the response marshalling happens after routing has completed, 
so this failure is not routed through
+            // the error handler and would otherwise be invisible. Log it so 
operators can diagnose why the response
+            // could not be bound (see CAMEL-24594).
+            LOG.warn("Error marshalling REST DSL response body for exchange: 
{} due to: {}."
+                     + " This exception is set on the exchange to fail the 
response.",
+                    exchange, e.getMessage(), e);
             exchange.setException(e);
         }
 

Reply via email to