40.8. REST producer 绑定
REST producer 支持使用与 rest-dsl 一样使用 JSon 或 XML 的绑定。
例如,您可以在其他配置中使用 jetty with json 绑定模式来配置它:
restConfiguration().component("jetty").host("localhost").port(8080).bindingMode(RestBindingMode.json);
from("direct:start")
.to("rest:post:user");
restConfiguration().component("jetty").host("localhost").port(8080).bindingMode(RestBindingMode.json);
from("direct:start")
.to("rest:post:user");
然后,在使用 rest producer 调用 REST 服务时,它会在调用 REST 服务前自动将 POJO 绑定到 json:
UserPojo user = new UserPojo();
user.setId(123);
user.setName("Donald Duck");
template.sendBody("direct:start", user);
UserPojo user = new UserPojo();
user.setId(123);
user.setName("Donald Duck");
template.sendBody("direct:start", user);
在上面的示例中,我们发送了 POJO 实例 UserPojo 作为消息正文。由于我们已经在其他配置中开启了 JSon 绑定,因此,在调用 REST 服务之前,OVAM 会从 OVA 变为 JSon。
但是,如果您也要对响应消息执行绑定(例如,将 REST 服务发送作为响应),则需要配置 outType 选项,以指定 VRRP 的 classname 从 JSon 到 SNI 的 unmaral。
例如,如果 REST 服务返回 JSon 有效负载,它将绑定到 com.foo.MyResponsePojo,您可以按照如下所示配置:
restConfiguration().component("jetty").host("localhost").port(8080).bindingMode(RestBindingMode.json);
from("direct:start")
.to("rest:post:user?outType=com.foo.MyResponsePojo");
restConfiguration().component("jetty").host("localhost").port(8080).bindingMode(RestBindingMode.json);
from("direct:start")
.to("rest:post:user?outType=com.foo.MyResponsePojo");
注意
如果您希望 POJO 绑定发生,对于从调用 REST 服务接收的响应消息,则需要配置 outType 选项。