JAXB does not compile @XmlJavaTypeAdapter

I have the following class that I need to serialize as XML:

@XmlAccessorType(XmlAccessType.FIELD)
public class Position {

    @XmlElement(name = "Quantity", required = true)
    private DecimalQuantity quantity;

    ...
}

I put the XmlJavaTypeAdapter into the DecimalQuantity class because I want it to be serialized just like BigDecimal without the DecimalQuantity shell.

@XmlJavaTypeAdapter(DecimalQuantityAdapter.class)
@Embeddable
public class DecimalQuantity {

    private BigDecimal value;

    ...

}

Here's a very simple DecimalQuantityAdapter class:

public class DecimalQuantityAdapter
        extends XmlAdapter<BigDecimal, DecimalQuantity> {

    public DecimalQuantity unmarshal(BigDecimal val) throws Exception {
        return new DecimalQuantity(val);
    }

    public BigDecimal marshal(DecimalQuantity val) throws Exception {
        return val.getValue();
    }
}

I have a unit test that shows that the adapter is working correctly. The following Order object, which has DecimalQuantity, is serialized correctly (note that this test class looks almost identical to the Position class above):

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Order")
public class Order {

    @XmlElement(name = "Quantity", required = true)
    private DecimalQuantity quantity;

    ...

}

It is serialized as shown below - there is no wrapper around the decimal number - life is good!

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Order>
    <Quantity>10.2</Quantity>
</Order>

, DecimalQuantity maven. , Position, , maven. -, Position, maven. - DecimalQuantity, , DecimalQuantity DecimalQuantityAdapter. , :

Caused by: javax.xml.bind.JAXBException:
class org.archfirst.common.quantity.DecimalQuantity nor any of its super class is known to this context.
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:594)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:648)
    ... 53 more

@XmlJavaTypeAdapter , JAXB . - @XmlSeeAlso ({DecimalQuantity.class}) Position. , () :

<Quantity xsi:type="ns2:decimalQuantity" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

, ? , - JAXB DecimalQuantity DecimalQuantityAdapter /.

.

+3
2

, , , . unit test JAXB Java, (-) JAXB GlassFish. -, , GlassFish (2.2.1.1), . , unit test jaxb-impl-2.2.1.1.jar. , ​​ JAXB (2.2.3-1), , GlassFish (. ).

+1

, XmlJavaTypeAdapter , DecimalQuantity. , , , , JAXB .

, @XmlJavaTypeAdapter? , , , , , ? ?

, , :

@XmlSeeAlso({DecimalQuantity.class})

, .

XML- / , , ?

0

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


All Articles