Java – customizing JAXB bindings for primtive

I have several patterns, and I'm generating a JAXB binding because it's free to use XS: integer I want to bind these values to long / long instead of the default BigInteger Unfortunately, I have no ability to modify the mode Adding a simple declaration to my binding file will cause XS: integer to bind to long in all cases, even if it is a required value:

<jaxb:javaType xmlType="xs:integer" name="long" />

How do I bind XS: integer to a primitive when a field is required?

Solution

It's simple. You just need to be in There are two different bindings in the JXB file, and use the correct XPath selector to find the element that needs to be mapped to integer and another XPath selector that will find the element to be mapped to int

Because you haven't published it yet XSD and XJB file, so I'll show how everything works in a small example Suppose we have the following XSD file

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="user">
         <xs:complexType>
             <xs:sequence>
                 <xs:element type="xs:integer" name="age" minOccurs="1"/>
                 <xs:element type="xs:integer" name="balance" minOccurs="0"/>
             </xs:sequence>
         </xs:complexType>
    </xs:element>
</xs:schema>

So from XSD we can see that we have a user object definition with the required age attribute (we can still use the use = "optional" attribute, but it doesn't matter) and the balance attribute is optional

So suppose we want to map age to Java int and balance to integer

Therefore, we only need a binding file, which will find all nodes with type attribute equal to XS: integer and minoccurs attribute equal to 1, apply some custom mapping rules, and perform the same operation on all nodes with minoccurs attribute equal to 0

<jxb:bindings version="2.0" 
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <jxb:bindings schemaLocation="schema.xsd">
        <!---Find all optional integers and map them to java.lang.Integer -->
        <jxb:bindings node="//xs:element[@minOccurs='0' and @type='xs:integer']" multiple="true">
            <xjc:javaType name="java.lang.Integer" adapter="adapters.IntegerAdapter" />
        </jxb:bindings>

        <!---Find all required integers and map them to primitive int -->
        <jxb:bindings node="//xs:element[@minOccurs='1' and @type='xs:integer']" multiple="true">
            <xjc:javaType name="int" adapter="adapters.IntAdapter" />
        </jxb:bindings>

    </jxb:bindings>
</jxb:bindings>

After running the build (remember to enable vendor customization and add the - extension command line parameter), you will have something like this

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "",propOrder = {
"age","balance"
})
@XmlRootElement(name = "user")
public class User {

    @XmlElement(required = true,type = String.class)
    @XmlJavaTypeAdapter(IntAdapter.class)
    @XmlSchemaType(name = "integer")
    protected int age;
    @XmlElement(type = String.class)
    @XmlJavaTypeAdapter(IntegerAdapter.class)
    @XmlSchemaType(name = "integer")
    protected Integer balance;

    // getters setters will be here
}

Also, please note that I did not add the source of the adapter Integeradapter and adapters Intadapter (this part is for you)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>