示例代码来自http://camel.apache.org/xstream.html
If you would like to configure the XStream instance used by the Camel for the message transformation, you can simply pass a reference to that instance on the DSL level.
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
构造函数只接受字符串。如何在camel中配置自定义com.thoughtworks.xstream.XStream
?
我不想使用 XML,我的应用程序正在使用 Spring。
最佳答案
如果你想快速完成它,你可以重定向到一个编码“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);
}
}
关于java - 如何将 XStream 引用传递给 Camel 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28605782/