I am trying to convert xml to object and object to xml using marsheller. But I am getting the below error.
Error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XMLConverter' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.oxm.castor.CastorMarshaller' to required type 'javax.xml.bind.Marshaller' for property 'marshaller'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.oxm.castor.CastorMarshaller] to required type [javax.xml.bind.Marshaller] for property 'marshaller': no matching editors or conversion strategy found
Code Snippet:
App.java
ApplicationContext appContext = new ClassPathXmlApplicationContext("App.xml");
XMLConverter converter = (XMLConverter) appContext.getBean("XMLConverter");
Message message = new Message();
List<HeaderPojo> headerList = new ArrayList<HeaderPojo>();
List<InputInfoPojo> inputInfoList = new ArrayList<InputInfoPojo>();
List<MessageBody> MessageBody = new ArrayList<MessageBody>();
HeaderPojo Header = new HeaderPojo();
Header.setMessageName("Hello");
Header.setMessageDesc("hi");
Header.setMessageType("Request");
InputInfoPojo InputInfo = new InputInfoPojo();
InputInfo.setProviderID("1234");
InputInfo.setProviderLocation("1");
InputInfo.setProviderRequestSource("sff");
headerList.add(Header);
inputInfoList.add(InputInfo);
MessageBody Body = new MessageBody();
Body.setInputInfo(inputInfoList);
MessageBody.add(Body);
message.setHeader(headerList);
message.setBody(MessageBody);
try {
System.out.println("------- Object to XML -----------\n");
JAXBContext jaxbContext = JAXBContext.newInstance(Message.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(message, System.out);
System.out.println("\n------- XML to Object -----------\n");
String xml = "<message><body><inputInfo><providerID>1234</providerID><Location>1</Location><providerRequestSource>sff</providerRequestSource></inputInfo></body><header><messageDesc>hello</messageDesc><messageName>FeeSchedules</messageName><messageType>Request</messageType></header></message>";
StringReader reader = new StringReader(xml);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Message elementsOut = (Message) jaxbUnmarshaller.unmarshal(reader);
System.out.println(elementsOut);
} catch (JAXBException e) {
e.printStackTrace();
}
}
xmlconverter.java:
private Marshaller marshaller;
private Unmarshaller unmarshaller;
public Marshaller getMarshaller() {
return marshaller;
}
public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}
public Unmarshaller getUnmarshaller() {
return unmarshaller;
}
public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public String convertFromObjectToXML(Object object)
throws IOException {
String xmlString = null;
StringWriter strWriter = null;
try {
strWriter = new StringWriter();
getMarshaller().marshal(object, new StreamResult(strWriter));
xmlString = strWriter.toString();
} finally {
if (strWriter != null) {
strWriter.close();
}
}
return xmlString;
}
public Object convertFromXMLToObject(String xmlString) throws IOException {
StringReader strReader = null;
try {
strReader = new StringReader(xmlString);
return getUnmarshaller().unmarshal(new StreamSource(strReader));
} finally {
if (strReader != null) {
strReader.close();
}
}
}
app.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="XMLConverter" class="com.java.core.XMLConverter">
<property name="marshaller" ref="castorMarshaller" />
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" >
</bean>
</beans>
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor</artifactId>
<version>1.2</version>
</dependency>
<!-- Castor need this -->
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.8.1</version>
</dependency>
Please suggest me how to solve this error
I restarted the server and it started working. :) This code works fine if someone wants to refer in future.