You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2026/07/11 07:00:57 UTC
(camel) 01/01: CAMEL-24001: Fix RestBindingAdvice setting Content-Type on body-less responses
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch fix/CAMEL-24001
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 6b31dc7cc0497101a9d81ec0400a373dc5f45b9b
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Jul 11 09:00:40 2026 +0200
CAMEL-24001: Fix RestBindingAdvice setting Content-Type on body-less responses
Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
Signed-off-by: Claus Ibsen <cl...@gmail.com>
---
.../rest/FromRestGetContentTypeEmptyBodyTest.java | 97 ++++++++++++++++++++++
.../camel/support/processor/RestBindingAdvice.java | 35 +++-----
2 files changed, 110 insertions(+), 22 deletions(-)
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetContentTypeEmptyBodyTest.java b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetContentTypeEmptyBodyTest.java
new file mode 100644
index 000000000000..336a5668e018
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetContentTypeEmptyBodyTest.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.rest;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Registry;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class FromRestGetContentTypeEmptyBodyTest extends ContextTestSupport {
+
+ @Override
+ protected Registry createCamelRegistry() throws Exception {
+ Registry jndi = super.createCamelRegistry();
+ jndi.bind("dummy-rest", new DummyRestConsumerFactory());
+ return jndi;
+ }
+
+ @Test
+ public void testNoContentTypeOnEmptyBody() {
+ Exchange out = template.request("seda:delete-say-hello", exchange -> {
+ // no body
+ });
+
+ assertNotNull(out);
+ assertNull(out.getMessage().getBody(), "Body should be null");
+ assertNull(out.getMessage().getHeader(Exchange.CONTENT_TYPE),
+ "Content-Type should not be set on a body-less response");
+ }
+
+ @Test
+ public void testContentTypeSetOnNonEmptyBody() {
+ Exchange out = template.request("seda:get-say-hello", exchange -> {
+ // no body
+ });
+
+ assertNotNull(out);
+ assertEquals("{ \"name\" : \"Donald\" }", out.getMessage().getBody());
+ assertEquals("application/json", out.getMessage().getHeader(Exchange.CONTENT_TYPE));
+ }
+
+ @Test
+ public void testContentTypeJsonWhenMultiValueProduces() {
+ Exchange out = template.request("seda:get-say-multi", exchange -> {
+ // no body
+ });
+
+ assertNotNull(out);
+ assertEquals("{ \"name\" : \"Donald\" }", out.getMessage().getBody());
+ assertEquals("application/json", out.getMessage().getHeader(Exchange.CONTENT_TYPE));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ restConfiguration().host("localhost");
+
+ rest("/say/hello")
+ .produces("application/json")
+ .delete().to("direct:delete")
+ .get().to("direct:hello");
+
+ rest("/say/multi")
+ .produces("application/json,text/plain")
+ .get().to("direct:hello");
+
+ from("direct:delete")
+ .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(204))
+ .setBody(constant(null));
+
+ from("direct:hello")
+ .setBody(constant("{ \"name\" : \"Donald\" }"));
+ }
+ };
+ }
+}
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 073743b90f5d..2dbc10e6e159 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
@@ -392,6 +392,11 @@ public class RestBindingAdvice extends ServiceSupport implements CamelInternalPr
// need to prepare exchange first
ExchangeHelper.prepareOutToIn(exchange);
+ // is the body empty (no Content-Type should be set for body-less responses such as 204 No Content)
+ if (exchange.getMessage().getBody() == null) {
+ return;
+ }
+
// ensure there is a content type header (even if binding is off)
ensureHeaderContentType(produces, isXml, isJson, exchange);
@@ -405,11 +410,6 @@ public class RestBindingAdvice extends ServiceSupport implements CamelInternalPr
return;
}
- // is the body empty
- if (exchange.getMessage().getBody() == null) {
- return;
- }
-
String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
// need to lower-case so the contains check below can match if using upper case
contentType = contentType.toLowerCase(Locale.US);
@@ -484,27 +484,18 @@ public class RestBindingAdvice extends ServiceSupport implements CamelInternalPr
}
private void ensureHeaderContentType(String contentType, boolean isXml, boolean isJson, Exchange exchange) {
- // favor given content type
- if (contentType != null) {
- String type = ExchangeHelper.getContentType(exchange);
- if (type == null) {
- exchange.getIn().setHeader(Exchange.CONTENT_TYPE, contentType);
- }
+ String type = ExchangeHelper.getContentType(exchange);
+ if (type != null) {
+ return;
}
- // favor json over xml
+ // favor json over xml as a concrete single media type
if (isJson) {
- // make sure there is a content-type with json
- String type = ExchangeHelper.getContentType(exchange);
- if (type == null) {
- exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
- }
+ exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
} else if (isXml) {
- // make sure there is a content-type with xml
- String type = ExchangeHelper.getContentType(exchange);
- if (type == null) {
- exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/xml");
- }
+ exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/xml");
+ } else if (contentType != null) {
+ exchange.getIn().setHeader(Exchange.CONTENT_TYPE, contentType);
}
}