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

  • Castor unmarshalling problem

    Hello everybody,
    I'm having a problem with Castor unmarshalling: it doesn't resolve correctly the name of classes placed in different packages.

    I explain: in my project i got two packages
    Code:
    myProject.entities
                           MyEntity
    myProject.schema
                           SaveMyEntityRequest
    The object SaveMyEntityRequest has a field of type MyEntity. When I send this request, Castor tries to unmarshall the inner property of SaveMyEntityRequest as a myProject.schema.MyEntity object resulting in a MarshaException, even if in the Castor mapping file I specifield

    Code:
    <class name="myProject.entities.MyEntity" auto-complete="false">
      <map-to name="MyEntity"....>
      ......
    </class>
    
    <class name="myProject.schema.saveMyEntityRequest"  auto-complete="false">
      <map-tp name="SaveMyEntityRequest"......>
      <field name="entity" type="myProject.entities.MyEntity">
        <bind-xml name="entity" node="element" />
      </field>
      ......
    </class>
    The error message i receive is

    Code:
    Castor unmarshalling exception: unable to find FieldDescriptor for 'entity' in
    ClassDescriptor of SaveMyEntityRequest; nested exception is
    org.exolab.castor.xml.MarshalException: unable to find FieldDescriptor for 'entity' in
    ClassDescriptor of SaveMyEntityRequest
    I know it looks like a Castor issue, but I didn't find any info in Castor documentation.

    Thanks
    Ste

  • #2
    This has to do with a class description resolver. I'm now searching about how to implement it. If you found out, could you please post it?
    Kurt

    Comment


    • #3
      Castor unmarshalling wasn't working with spring's objects so I had to switch to "non-spring" code, shown below. I tried using casing the unmarshaller to the oxm.castor.CastoerMarshaller.setTargetClass, but that didn't work.
      Code:
      public Object parseString(StringBuffer xMLToConvert, java.lang.Class<?> cl) {
      		Object tTI = new Object();
      		Logger logger = Logger.getLogger("MCMApplication");
      		StringReader inStr = null;
      		XMLContext context = new XMLContext();
      		org.exolab.castor.xml.Unmarshaller unm = context.createUnmarshaller();
      		unm.setClass(cl);
      		try {
      			inStr = new StringReader(xMLToConvert.toString());
      			//tTI = this.getUnmarshaller().unmarshal(new StreamSource(inStr));
      
      			tTI = unm.unmarshal(inStr);	
      
      		} catch (MarshalException e) {
      			e.printStackTrace();
      		} catch (ValidationException e) {
      			e.printStackTrace();
      		}
      		finally {
      			if (inStr != null) {
      				inStr.close();
      			}
      		}
      
      		logger.trace("Finished Marshalling...");
      
      		return tTI;
      	}
      Does anyone have any idea how I could do the same thing with Spring? I was originally using the one line commented out in the try/catch block to unmarshal and kept getting "could not find class" error.

      It should note that the commented line DOES work fine with a mapping file.
      Kurt

      Comment


      • #4
        Hi Kurt
        I yet found a solution.
        You have to specify the schema in the field of the request.

        Code:
        <class name="myProject.entities.MyEntity" auto-complete="false">
          <map-to name="MyEntity".... ns-uri="http://uri.com">
          ......
        </class>
        
        <class name="myProject.schema.saveMyEntityRequest"  auto-complete="false">
          <map-tp name="SaveMyEntityRequest"......>
          <field name="entity" type="myProject.entities.MyEntity">
            <bind-xml name="entity" node="element" xmlns="http://uri.com" />
          </field>
          ......
        </class>
        The DTD validation could mark it as an error, but it is not.
        You could comment the DTD specification.
        With this, I didn't write any code to customize the (un)marshalling: I used the standard Castor marshaller provided by Spring.

        Hope this can help you
        Ste

        PS: Furthermore, I found out that you'd better specify always the xmlns when the field is a complex object that has its own mapping file, above all if you are working with multiple namespaces.
        Last edited by zakhrim; Sep 11th, 2010, 03:50 AM.

        Comment

        Working...
        X