如何将 XStream 引用传递给骆驼以进行编组
Howto pass XStream reference into camel for marshalling
示例代码来自 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。
如果您想快速完成它,而不是通过 "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);
}
}
我的解决方案。
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");
确保使用
import org.apache.camel.dataformat.xstream.XStreamDataFormat;
而不是
import org.apache.camel.model.dataformat.XStreamDataFormat;
对于class
new XStreamDataFormat(xStream)
示例代码来自 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。
如果您想快速完成它,而不是通过 "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);
}
}
我的解决方案。
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");
确保使用
import org.apache.camel.dataformat.xstream.XStreamDataFormat;
而不是
import org.apache.camel.model.dataformat.XStreamDataFormat;
对于class
new XStreamDataFormat(xStream)