79.6. 消耗 REST 请求 - Simple Binding Style
从 Camel 2.11 开始提供
默认 绑定风格是低级,要求用户手动处理进入路由的 MessageContentsList 对象。因此,它将路由逻辑与 JAX-RS 操作的方法签名和参数索引紧密耦合。微不足道,很困难且容易出错。
相反,simpleConsumer 绑定风格会执行以下映射,以便在 Camel 消息中 更方便地访问请求数据 :
- JAX-RS 参数(@HeaderParam、@QueryParam 等)注入为 IN 消息标头。标头名称与注解值匹配。
-
请求实体(POJO 或其他类型)成为 IN 消息正文。如果无法在 JAX-RS 方法签名中识别单个实体,它将回退到原始
MessageContentsList。 -
二进制
@Multipartbody parts become IN message attachments, supportDataHandler,InputStream,DataSource和 CXF 的Attachment类。 -
非二进制
@Multipartbody 部分映射为 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 响应中。
79.6.1. 启用简单绑定样式 复制链接链接已复制到粘贴板!
通过将消费者端点中的 bindingStyle 参数设置为 SimpleConsumer,可以激活此绑定风格:
from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer")
.to("log:TEST?showAll=true");
from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer")
.to("log:TEST?showAll=true");
79.6.2. 使用不同方法签名的请求绑定示例 复制链接链接已复制到粘贴板!
以下是方法签名列表以及简单绑定的预期结果。
公共响应 doAction (BusinessObject request);
Request payload 被放在 IN message body 中,替换原始 MessageContentsList。
公共响应 doAction (BusinessObject request, @HeaderParam ("abcd") String abcd, @QueryParam ("defg") String defg"); Request payload placed in IN messageContentsList.请求参数都映射为 IN 消息标头,其名称为 abcd 和 defg。
公共响应 doAction (@HeaderParam ("abcd") String abcd, @QueryParam ("defg") String defg); Both request params mapped as IN message params with name abcd and defg.原始 MessageContentsList 被保留,即使它只包含 2 参数。
公共响应 doAction (@Multipart (value="body1") BusinessObject request, @Multipart (value="body2") BusinessObject request2); 第一个参数作为名称 body1 的标头传输,第二个参数被映射为标头正文2。原始 MessageContentsList 保留为 IN 消息正文。
公共响应 doAction (InputStream abcd); InputStream is unwrapped from the MessageContentsList and reserved as the IN message body.
公共响应 doAction (DataHandler abcd); DataHandler 从 MessageContentsList 中取消换行,并保留为 IN 消息正文。
79.6.3. 更多简单绑定样式示例 复制链接链接已复制到粘贴板!
使用此方法给定 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):
将打印消息:
Request: type=gold, active=true, customerData=<Customer.toString() representation>
Request: type=gold, active=true, customerData=<Customer.toString() representation>
有关如何处理请求和写入响应的更多示例,可在此处找到。