Hi,
I’m migrating some Camel projects from Camel 2.17 to 3.4.
I have a Camel REST DSL based route in which I try to manually set the
”Content-Type” HTTP header of the response. However, no matter what values I
try to set in the route, the only values shown in the responses are either
”application/xml” or ”application/json” (depending on if I requested XML or
JSON in the Accept HTTP header when calling the REST endpoint).
My route is implemented in the following way (and I always call the REST
endpoint with the ”Accept” HTTP header set to either
”application/my.dummy.mediatype+xml” or ”application/my.dummy.mediatype+json”):
restConfiguration()
.component("servlet")
.skipBindingOnErrorCode(true)
.bindingMode(RestBindingMode.json_xml)
.endpointProperty("servletName", "CamelServletDummy");
rest().tag("Dummy")
.get("dummy")
.produces("application/my.dummy.mediatype+xml,application/my.dummy.mediatype+json")
.outType(DummyJaxbAnnotatedClass.class)
.route()
.process(exchange -> {
exchange.getIn().setBody(new DummyJaxbAnnotatedClass());
})
.setHeader(Exchange.CONTENT_TYPE, header("Accept"))
.endRest();
The main difference between 2.17 and 3.4 seems to be that the following code
has been added to JaxbDataFormat in 3.4 (for implementation details, see
https://github.com/apache/camel/blob/camel-3.4.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java#L174:
if (contentTypeHeader) {
if (exchange.hasOut()) {
exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/xml");
} else {
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/xml");
}
}
Similar code has beed added to JacksonDataFormat (for implementation details,
see
https://github.com/apache/camel/blob/camel-3.4.x/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java#L160).
contentTypeHeader is set to true as default in both JaxbDataFormat and
JacksonDataFormat.
I have been trying to find a way to configure this but hasn’t found any. Is
there a way to configure contentTypeHeader when implementing a route the way I
do? If not, is it possible to add this option in upcoming Camel versions? Is
there any workarounds without having to change binding mode to ”off” and
”manually” handle the marshalling to either XML or JSON messages?
Thanks in advance,
Calle