来自http://camel.apache.org/xstream.html
的示例代码如果您想配置Camel用于消息转换的XStream实例,您只需在DSL级别上传递对该实例的引用。
XStream xStream = new XStream();
xStream.aliasField("money", PurchaseOrder.class, "cash");
// new Added setModel option since Camel 2.14
xStream.setModel("NO_REFERENCES");
...
from("direct:marshal").
marshal(new XStreamDataFormat(xStream)).
to("mock:marshaled");
但是这段代码是错误的,因为org.apache.camel.model.dataformat.XStreamDataFormat
构造函数只接受String。如何在camel中配置自定义com.thoughtworks.xstream.XStream
?
我不想使用XML,我的应用程序使用的是Spring。
答案 0 :(得分:1)
如果你想快速完成它,而不是通过“marshal”,你可以重定向到一个编组“bean”,它将以你需要的方式进行游戏。
from(...).bean('marshallingBean').to(...)
@Autowired
FooDeserializer fooDeserializer;
@Bean
public RouteBuilder route() {
return new RouteBuilder() {
public void configure() {
from("direct:marshal")
.bean(fooDeserializer)
.to("mock:marshaled");
}
};
}
FooDeserializer.java
@Component
public class FooDeserializer {
private final XStream xStream;
public FooDeserializer() {
xStream = new XStream();
xStream.aliasField("money", PurchaseOrder.class, "cash");
}
public Foo xmlToFoo(String xml) {
return (Foo) xStream.fromXML(xml);
}
}
答案 1 :(得分:1)
确保使用
import org.apache.camel.dataformat.xstream.XStreamDataFormat;
而不是
import org.apache.camel.model.dataformat.XStreamDataFormat;
为班级
new XStreamDataFormat(xStream)
答案 2 :(得分:0)
我的解决方案。
final XStream xStream = new XStream();
xStream.aliasField("money", PurchaseOrder.class, "cash");
from("direct:marshal")
.process(new Processor() {
@Override
public void process(Exchange exchange) {
String xmlConverted = xStream.toXML(exchange.getIn().getBody());
exchange.getIn().setBody(xmlConverted);
}
}).to("mock:marshaled");