Announcement Announcement Module
Collapse
No announcement yet.
Spring oxm castor unmarshalling Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring oxm castor unmarshalling

    Hi i am new to spring oxm. We are working on a project where i need to replace the castor marshalling logic to spring oxm castor marshalling logic. I have done a sample poc on spring oxm using castormarshaller. I have configured the castor marshaller using spring oxm. I have a mapping file defined and i have a java class. Now in my application there are two classes with the same name and both are defined in the mapping file for unmarshalling respectively. When i am trying to unmarshall the xml string i am getting the classcast exception. pls see the below mapping file. when i am trying to unmarshall the string to com.ameriprise.finaccounts.shr.beans.value.data.ac count.SecIdVO it is taking the first occured class from mapping file and giving the class cast exception.

    In spring oxm how to identify the different context paths. In castor marshaller working fine but castor marshaller using spring oxm i am getting the problem of taking the context paths. If i am not using the <map-to xml="SECID"/> then also i am getting root class not found issue in spring oxm. How to resove the issue. please let me know if u need any other information

    <SECID><UNIQUEID>hello</UNIQUEID><UNIQUEIDTYPE>test</UNIQUEIDTYPE></SECID>

    mapping file


    <mapping>
    <class name="com.ameriprise.finaccounts.shr.beans.value.d ata.temp.SecIdVO" >
    <map-to xml="SECID"/>
    <field name="uniqueId" type="string" required="false" direct="false" transient="false">
    <bind-xml name="UNIQUEID" node="element"/>
    </field>
    <field name="uniqueIdType" type="string" get-method="retrieveUniqueIdType" set-method="setUniqueIdType">
    <bind-xml name="UNIQUEIDTYPE" node="element"/>
    </field>
    </class>

    <class name="com.ameriprise.finaccounts.shr.beans.value.d ata.account.SecIdVO" >
    <map-to xml="SECID"/>
    <field name="uniqueId" type="string" required="false" direct="false" transient="false">
    <bind-xml name="UNIQUEID" node="element"/>
    </field>
    <field name="uniqueIdType" type="string" get-method="retrieveUniqueIdType" set-method="setUniqueIdType">
    <bind-xml name="UNIQUEIDTYPE" node="element"/>
    </field>
    </class>
    </mapping>



    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;

    import org.exolab.castor.mapping.Mapping;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;
    import org.springframework.core.io.Resource;
    import org.springframework.oxm.Marshaller;
    import org.springframework.oxm.Unmarshaller;
    import org.springframework.oxm.castor.CastorMarshaller;
    import org.springframework.oxm.ValidationFailureException ;

    import com.ameriprise.finaccounts.shr.beans.value.data.ac count.SecIdVO;

    public class Application {
    private static final String FILE_NAME = "secid1.xml";
    private SecIdVO secIdVO = new SecIdVO();
    private Marshaller marshaller;
    private Unmarshaller unmarshaller;

    public void setMarshaller(Marshaller marshaller) {
    this.marshaller = marshaller;
    }

    public void setUnmarshaller(Unmarshaller unmarshaller) {
    this.unmarshaller = unmarshaller;
    }

    public void saveSecIdVO() throws IOException {
    FileOutputStream os = null;
    try {
    os = new FileOutputStream(FILE_NAME);
    this.marshaller.marshal(secIdVO, new StreamResult(os));

    } finally {
    if (os != null) {
    os.close();
    }
    }
    }

    public void loadSecIdVO() throws IOException {
    FileInputStream is = null;
    SecIdVO sec;
    try {
    is = new FileInputStream(FILE_NAME);
    sec = (SecIdVO) this.unmarshaller.unmarshal(new StreamSource(is));
    System.out.println("*******sec>>>>" + sec.generateXML());
    } catch (Exception ex) {
    System.out.println("******ex>>>>>>>>>");
    ex.printStackTrace();
    } finally {
    if (is != null) {
    is.close();
    }
    }
    // System.out.println("*******secID*****" + this.secIdVO.generateXML());
    }


    /*public Mapping loadMapping() {
    String appBasePath = "C:\\Documents and Settings\\efc6287\\Desktop\\SpringOXM\\src\\resour ces\\xml\\mappingoutput.xml";
    Mapping mapping = new Mapping();
    try {
    mapping.loadMapping(appBasePath);

    } catch (Exception ex) {
    System.out
    .println("******mapping exception*****" + ex.getMessage());
    }
    return mapping;
    }*/

    public static void main(String[] args) throws IOException {
    ApplicationContext appContext = new ClassPathXmlApplicationContext(
    "applicationContext.xml");

    CastorMarshaller castorMarsh = (CastorMarshaller)appContext.getBean("castorMarsha ller");

    //castorMarsh.setMappingLocation(new File(FILE_NAME));

    Application application = (Application) appContext
    .getBean("application");
    ///System.out.println(application.isValid());
    application.saveSecIdVO();
    application.loadSecIdVO();
    }
    }

    Exception is
    java.lang.ClassCastException: com.ameriprise.finaccounts.shr.beans.value.data.te mp.SecIdVO cannot be cast to com.ameriprise.finaccounts.shr.beans.value.data.ac count.SecIdVO
    at Application.loadSecIdVO(Application.java:52)
    at Application.main(Application.java:91)
    Last edited by govindgembali; Apr 27th, 2011, 01:12 AM. Reason: Added stack trace
Working...
X