我正在尝试在CXFRS Camel路由中创建对REST Web服务调用的响应,但无论我做什么,对客户端的响应总是相同的200,而不是201.这是我的路由:
<route id="front-end">
<from uri="cxfrs:bean:myService" />
<setBody>
<constant>Will do...</constant>
</setBody>
<setHeader headerName="CamelHttpResponseCode">
<constant>201</constant>
</setHeader>
<setHeader headerName="Content-Type">
<constant>more/blah</constant>
</setHeader>
</route>
返回正文但忽略响应代码和内容类型。我做错了什么?
谢谢, 马特
答案 0 :(得分:3)
基本上,当将交换转换为实际的HTTP响应时,camel-cxfrs会覆盖在交换中设置的任何标头 见here:
exchange.getOut().setHeaders(binding.bindResponseHeadersToCamelHeaders(response, exchange));
这是因为DefaultCxfRsBinding期望jaxrs Response作为参数。
因此,要解决此问题,请使用自定义方法覆盖DefaultCxfRsBinding,以便从exchange.getIn()复制标头。
<cxf:rsServer id="MyService" address="/myAddress">
<cxf:binding><bean class="MyCustomCxfRsBinding" /></cxf:binding>
<cxf:serviceBeans>
<ref bean="myResourceWithJSR311Annotations" />
</cxf:serviceBeans>
</cxf:rsServer>
或者让你的camel路由返回jaxrs响应带标题,而不是在路由器或camel处理器中设置标头。什么
class HttpHeaderProcessor implements Processor
{
@Override
public void process(Exchange exchange) throws Exception
{
Message message = exchange.getIn();
Response response = convertToJaxRs(message);
exchange.getIn().setBody(response);
exchange.getIn().setHeader("Test", "Won't work unless DefaultCxfRsBinding is not replaced with a custom one");
}
private Response convertToJaxRs(Message message)
{
ResponseBuilder jaxrsResponseBuilder = Response.ok(message.getBody(), MediaType.APPLICATION_XML);
jaxrsResponseBuilder.header("header1", "you'll see this");
Response response = jaxrsResponseBuilder.build();
return response;
}
}
您的样本:
<route id="front-end">
<from uri="cxfrs:bean:myService" />
<setBody>
set it to Response.ok(your message).header(x, y).build()
</setBody>
您还可以使用Service bean返回带有标题的jaxrs.Response
<route id="front-end">
<from uri="cxfrs:bean:myService" />
<bean ref="myServiceImpl">