我写了三个实现XStream的类.
它编组/解组以下XML.
<Message calledProgram="XXX" programName="YYY">
<Fields>
<Field name="supplierName" value="ABC"/>
<Field name="supplierNo" value="123456"/>
<Field name="supplierCountry" value="AA"/>
<Field name="TermsOfDelivery" value="5"/>
<Field name="PaymentTerms" value="90"/>
<Field name="Currency" value="GBP"/>
</Fields>
</Message>
我如何在Mule中使用这些类?它是一个使用其他两个类的类,所以它只是一个类使用.
我将收到上面的XML.我想将它转换为Object.
它在普通Java中工作,但我如何在流中使用它.从骡子获得所有可能的例外情况.
Exception in thread "main" org.mule.module.launcher.DeploymentinitException: InstantiationException: xxx.com.Genericclass
at org.mule.module.launcher.application.DefaultMuleApplication.init(DefaultMuleApplication.java:220)
at org.mule.module.launcher.application.ApplicationWrapper.init(ApplicationWrapper.java:64)
at org.mule.module.launcher.DefaultMuleDeployer.deploy(DefaultMuleDeployer.java:46)
at org.mule.tooling.server.application.ApplicationDeployer.run(ApplicationDeployer.java:56)
at org.mule.tooling.server.application.ApplicationDeployer.main(ApplicationDeployer.java:88)
Caused by: org.mule.api.config.ConfigurationException: Error creating bean with name 'MyB24Flow1': Invocation of init method Failed; nested exception is org.mule.api.lifecycle.InitialisationException: Unable to initialize XStream (org.mule.api.lifecycle.InitialisationException) (org.mule.api.config.ConfigurationException)
at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:52)
at org.mule.config.builders.AbstractResourceConfigurationBuilder.configure(AbstractResourceConfigurationBuilder.java:78)
at org.mule.context.DefaultMuleContextFactory.createMuleContext(DefaultMuleContextFactory.java:80)
at org.mule.module.launcher.application.DefaultMuleApplication.init(DefaultMuleApplication.java:208)
... 4 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyB24Flow1': Invocation of init method Failed; nested exception is org.mule.api.lifecycle.InitialisationException: Unable to initialize XStream
at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.initializeBean(AbstractAutowireCapablebeanfactory.java:1455)
at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.doCreateBean(AbstractAutowireCapablebeanfactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBean(AbstractAutowireCapablebeanfactory.java:456)
at org.springframework.beans.factory.support.Abstractbeanfactory$1.getobject(Abstractbeanfactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.Abstractbeanfactory.doGetBean(Abstractbeanfactory.java:291)
at org.springframework.beans.factory.support.Abstractbeanfactory.getBean(Abstractbeanfactory.java:193)
at org.springframework.beans.factory.support.DefaultListablebeanfactory.preInstantiateSingletons(DefaultListablebeanfactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishbeanfactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.mule.config.spring.SpringRegistry.doInitialise(SpringRegistry.java:89)
at org.mule.registry.AbstractRegistry.initialise(AbstractRegistry.java:109)
... 14 more
Caused by: org.mule.api.lifecycle.InitialisationException: Unable to initialize XStream
at org.mule.module.xml.transformer.AbstractXStreamTransformer.initialise(AbstractXStreamTransformer.java:52)
at org.mule.processor.chain.AbstractMessageProcessorChain.initialise(AbstractMessageProcessorChain.java:79)
at org.mule.construct.AbstractFlowConstruct.initialiseIfInitialisable(AbstractFlowConstruct.java:309)
at org.mule.construct.AbstractPipeline.doInitialise(AbstractPipeline.java:161)
at org.mule.construct.AbstractFlowConstruct$1.onTransition(AbstractFlowConstruct.java:111)
at org.mule.construct.AbstractFlowConstruct$1.onTransition(AbstractFlowConstruct.java:105)
at org.mule.lifecycle.AbstractLifecycleManager.invokePhase(AbstractLifecycleManager.java:141)
at org.mule.construct.FlowConstructLifecycleManager.fireInitialisePhase(FlowConstructLifecycleManager.java:81)
at org.mule.construct.AbstractFlowConstruct.initialise(AbstractFlowConstruct.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.invokeCustomInitMethod(AbstractAutowireCapablebeanfactory.java:1581)
at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.invokeInitMethods(AbstractAutowireCapablebeanfactory.java:1522)
at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.initializeBean(AbstractAutowireCapablebeanfactory.java:1452)
... 25 more
更新:
虽然这不是我真正的代码.但是你的意思是OOTB?这也不起作用.尝试过没有File-2-String.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">
<flow name="MyXStreamFlow1" doc:name="MyXStreamFlow1">
<file:inbound-endpoint path="E:\JavaDev\Test" responseTimeout="10000" doc:name="File"/>
<logger level="INFO" doc:name="Logger"/>
<file:file-to-string-transformer doc:name="File to String"/>
<logger message="WTF" level="INFO" doc:name="Logger"/>
<mulexml:xml-to-object-transformer doc:name="XML to Object"/>
<logger level="INFO" doc:name="Logger"/>
<mulexml:object-to-xml-transformer doc:name="Object to XML"/>
<logger level="INFO" doc:name="Logger"/>
</flow>
这是我的主要java类.
package test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.io.xml.DomDriver;
@XStreamAlias("Message")
public class Genericclass {
@XStreamAlias("calledProgram")
@XStreamAsAttribute
private String calledProgram;
@XStreamAlias("programName")
@XStreamAsAttribute
private String programName;
@XStreamAlias("Fields")
private Fields flds = new Fields(
new Field("nameValue1", "valueValue1"),
new Field("nameValue2", "valueValue2"),
new Field("nameValue3", "valueValue2"));
private static XStream xstream;
public Genericclass() {
new Genericclass("calledProgram", "programName");
}
public Genericclass(String calledProgram, String programName) {
xstream = new XStream(new DomDriver());
xstream.processAnnotations(Genericclass.class);
this.calledProgram = calledProgram;
this.programName = programName;
}
public static void main(String[] args) {
Genericclass msg = new Genericclass();
// Unmarshall
FileInputStream file = null;
try {
file = new FileInputStream(
"E:\\JavaDev\\JRecordFiles\\Samples\\LevisTest.xml");
} catch (FileNotFoundException e) {
// Todo Auto-generated catch block
e.printstacktrace();
}
msg = (Genericclass) xstream.fromXML(file);
System.out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + xstream.toXML(msg));
}
}
–
package test;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
public class Field {
private String field;
@XStreamAsAttribute
private String name;
@XStreamAsAttribute
private String value;
/**
* @param field
*/
public Field(String name, String value) {
this.name = name;
this.value = value;
}
/**
* @return the field
*/
public String getField() {
return field;
}
/**
* @param field
* the field to set
*/
public void setField(String field) {
this.field = field;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the value
*/
public String getValue() {
return value;
}
/**
* @param value
* the value to set
*/
public void setValue(String value) {
this.value = value;
}
}
–
package test;
import java.util.Arrays;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
public class Fields {
@XStreamImplicit(itemFieldName = "Field")
private List<Field> fields;
public Fields(Field... field) {
this.fields = Arrays.asList(field);
}
/**
* @return the fields
*/
public List<Field> getFields() {
return fields;
}
/**
* @param fields
* the fields to set
*/
public void setFields(List<Field> fields) {
this.fields = fields;
}
}
如何在Mule中使用XStream?
解决方法:
问题
<mulexml:object-to-xml-transformer doc:name="Object to XML">
<mulexml:alias name="myAlias" class="com.my.package.MyClass"/>
</mulexml:object-to-xml-transformer>
并忽略所有其他注释.自2012年7月以来,官方的bug追踪器已经有一个issue,但直到现在还没有解决.
1.创建变压器
在Java中创建一个扩展AbstractMessageTransformer的自定义转换器:
public class ObjectToXml extends AbstractMessageTransformer {
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
XStream xstream = new XStream();
// The payload should be the object to transform into XML
Object object = message.getPayload();
// Register all annotations in the XStream instance
xstream.processAnnotations(object.getClass());
// Convert the object to XML and set it as payload
message.setPayload(xstream.toXML(object));
return message;
}
}
2.在Mule流程中使用自定义变换器
在您的流中添加Java Transfromer(而不是Java组件):
<custom-transformer class="com.my.package.ObjectToXml" doc:name="Object to XML"/>
而已!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。