0

Soap is a new technology for me. I get a Soap message request, this is a part of it

<patient>
                <dateOfBirth>
                    <year></year>
                    <month></month>
                    <day></day>
                </dateOfBirth>
                <firstName>Ivan</firstName>
                <lastName>Luku</lastName>       
            </patient>

This is xsd schema for this part.

xs:complexType name="personVO">
<xs:complexContent>
<xs:extension base="tns:tenantableVO">
<xs:sequence>
<xs:element name="dateOfBirth" type="tns:localDateVO"/>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

------------------------------------------
<xs:complexType name="localDateVO">
<xs:sequence>
<xs:element name="day" type="xs:int" nillable="true" minOccurs="0"/>
<xs:element name="month" type="xs:int" nillable="true" minOccurs="0"/>
<xs:element name="year" type="xs:int" nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

As you can see year, day, month in the dateOfBirth tag have empty value. This variable presented in java code like Integer type. What should I do to get null for this variable after got this request?

I tried to write annotation above the get methods, but it didn't work. When I wrote annotation @XmlAttribute it gives me null always whether there are values or not.

    @XmlAttribute
    public Integer getDay() {
        return day;
    }
    @XmlAttribute
    public Integer getMonth() {
        return month;
    }
    @XmlAttribute
    public Integer getYear() {
        return year;
    }

And I tried to do the same annotation for field, but it's give me error like this (Class has two properties of the same name)

1 Answers1

0

It looks like Skaffman's answer JAXB: how to make JAXB NOT to unmarshal empty string to 0 in the link in the description is the best option after all.

I annotated my fields like this:

@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(XmlStringToIntegerAdapter.class)
@XmlSchemaType(name = "integer")
private Integer year;
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(XmlStringToIntegerAdapter.class)
@XmlSchemaType(name = "integer")
private Integer month;
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(XmlStringToIntegerAdapter.class)
@XmlSchemaType(name = "integer")
private Integer day;

Added some annotations to the class declaration:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LocalDate

And created adapter class:

public class XmlStringToIntegerAdapter extends XmlAdapter<String, Integer> {
public Integer unmarshal(String value) {
    return new Integer(value);
}

public String marshal(Integer value) {
    if (value == null) {
        return null;
    }
    return value.toString();
}}