30.9. 消耗 REST 请求 - Simple Binding Style
从 Camel 2.11 开始
Default
绑定风格是低级的,要求用户手动处理来自路由的 MessageContentsList
对象。因此,它会把路由逻辑与 JAX-RS 操作的方法签名和参数索引紧密连接,这在某种程度上比较困难、困难和容易出错。
相反,SimpleConsumer
绑定样式执行以下映射,以便可以在 Camel Message 中更轻松地访问请求数据:
-
JAX-RS 参数(
@HeaderParam
、@QueryParam
等)注入 IN 消息标头。标头名称与注解值匹配。 -
请求实体(POJO 或其他类型)成为 IN 消息正文。如果无法在 JAX-RS 方法签名中识别单个实体,它将回退到原始的
MessageContentsList
。 -
二进制
@Multipart
正文部分成为 IN 消息附件,支持DataHandler
、InputStream
、DataSource
和 CXF 的Attachment
类。 -
非二进制
@Multipart
正文部分映射为 IN 消息标头。标头名称与 Body Part 名称匹配。
另外,以下规则适用于 Response 映射 :
-
如果消息正文类型与
javax.ws.rs.core.Response
(user-built response)不同,则创建新的响应
,并且消息正文设置为实体(因此其不是 null)。响应状态代码从Exchange.HTTP_RESPONSE_CODE
标头获取,如果不存在,则默认为 200 OK。 -
如果消息正文类型等于
javax.ws.rs.core.Response
,这表示用户已构建自定义响应,因此它会被遵守,并成为最终响应。 -
在所有情况下,自定义或默认
HeaderFilterStrategy
允许的 Camel 标头都添加到 HTTP 响应中。
30.9.1. 启用 Simple Binding Style
通过将消费者端点中的 bindingStyle
参数设置为 SimpleConsumer
,可以激活这个绑定风格:
from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer") .to("log:TEST?showAll=true");
from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer")
.to("log:TEST?showAll=true");
30.9.2. 使用不同方法签名的请求绑定示例
以下是方法签名列表以及简单绑定的预期结果:
-
公共响应 doAction (BusinessObject 请求);
:请求有效负载放置在波be IN 消息正文中,替换原始 MessageContentsList。 -
公共响应 doAction (BusinessObject 请求, @HeaderParam ("abcd") String abcd, @QueryParam ("defg") String defg");
请求有效负载放置在 IN 消息正文中,替换原始MessageContentsList
。两个请求参数都映射为 IN 消息标头,其名称为 "abcd" 和 "defg "。 -
公共响应 doAction (@HeaderParam ("abcd") String abcd, @QueryParam ("defg") String defg);
: 两个请求参数都映射为名为 "abcd" 和 "defg" 的 IN 消息标头。原始MessageContentsList
被保留,即使它只包含两个参数。 -
公共响应 doAction (@Multipart (value="body1") BusinessObject 请求, @Multipart (value="body2") BusinessObject request2);
: 第一个参数作为名为 "body1" 的标头传输,第二个参数被映射为标题 "body2 "。原始MessageContentsList
保留为 IN message body。 -
公共响应 doAction (InputStream abcd);
:InputStream
is unwrapped from theMessageContentsList
and preserved as the IN message body. -
Public Response doAction (DataHandler abcd);
: DataHandler is unwrapped from theMessageContentsList
and preserved as the IN message body.
30.9.3. Simple Binding Style 的示例
使用此方法给定 JAX-RS 资源类:
@POST @Path("/customers/{type}") public Response newCustomer(Customer customer, @PathParam("type") String type, @QueryParam("active") @DefaultValue("true") boolean active) { return null; }
@POST @Path("/customers/{type}")
public Response newCustomer(Customer customer, @PathParam("type") String type, @QueryParam("active") @DefaultValue("true") boolean active) {
return null;
}
通过以下路由提供服务:
from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer") .recipientList(simple("direct:${header.operationName}")); from("direct:newCustomer") .log("Request: type=${header.type}, active=${header.active}, customerData=${body}");
from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer")
.recipientList(simple("direct:${header.operationName}"));
from("direct:newCustomer")
.log("Request: type=${header.type}, active=${header.active}, customerData=${body}");
以下带有 XML 有效负载的 HTTP 请求(代表客户 DTO 是 JAXB-annotated):
POST /customers/gold?active=true Payload: <Customer> <fullName>Raul Kripalani</fullName> <country>Spain</country> <project>Apache Camel</project> </Customer>
POST /customers/gold?active=true
Payload:
<Customer>
<fullName>Raul Kripalani</fullName>
<country>Spain</country>
<project>Apache Camel</project>
</Customer>
将打印消息:
Request: type=gold, active=true, customerData=<Customer.toString() representation>
Request: type=gold, active=true, customerData=<Customer.toString() representation>