com.sun.istack.SAXException2 : Instance … is substituting “java.lang.Object”, but … is bound to an anonymous type

前端 未结 3 2084
梦谈多话
梦谈多话 2021-01-18 03:41

I\'m upgrading a project to jaxb 2.2.7 from version 1.x.

I\'ve got the app working some of the time, but on some responses I see this:

java.lang.Runt         


        
3条回答
  •  一个人的身影
    2021-01-18 04:18

    The case that I noted does not depend on definitions in xsd-schema.

    The actual problem was that we have Java classes that extend from a class generated from xml-schema.

    And these classes are annotated with @XmlType(name = "") to make them anonymous (i.e. that generated tag does not contain xsi:type attribute and the generated xml-file remains valid for the initial-schema.

    I found the clue to this solution in java, xsd & marshalling: jre bug, my fault or xsd issues?.

    Since I can't modify the xsd (it is too complex and is already shared to the clients of our APIs), the solution is:

    • Make a jaxb-binder that excludes the complexType to be generated into Java file and says JAXB to use the existing class instead. Binding file looks like this:

      
          
              
          
      
      

    • In existing class, the class that we need to extend is NOT a JAXB-annotated class, just an abstract class:

    
        package my.existing.class;
    
        public abstract class TheClassFromWhichYouWantToExtend {
        }
    
    • The field that links to this class from the parent class is annotated with @XmlAnyElement(lax = true):
    
        package my.existing.class;
    
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(...)
        public class TheClassYouWantToExclude {
    
            // ...
            @XmlAnyElement(lax = true)
            protected TheClassFromWhichYouWantToExtend theClassFromWhichYouWantToExtend;
        }
    

提交回复
热议问题