Setting jaxb bindings for primates

I have several schemes in which I create jaxb bindings that use liberal use xs:integer. I want these values ​​to be bound to long/ longrather than the default BigInteger. Unfortunately, I do not have the ability to change the schema. Adding a simple declaration to my bindings file causes it to xs:integerbind to longin all cases, even if this is the required value:

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

How can I get xs:integerto bind to a primitive when a field is required?

+4
source share
1 answer

, .jxb XPath, , Integer, XPath, , int.

.xsd .xjb, , . , .xsd

<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>

, .xsd , user, age, ( use = "" , ) balance, .

, , , age Java int balance Integer.

, , , type xs:integer minOccurs, 1, - , minOccurs, 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>

( , -extension), - .

@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
}

, adapters.IntegerAdapter adapters.IntAdapter, )

0

Source: https://habr.com/ru/post/1683200/


All Articles