简体   繁体   中英

JAXB - Prevent nillable attributes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"

Here is my problem: I need the name tag of by object to be present in the XML but without the nillable attributes : In short like <name/> . Here is the code for the object. If name is null, I do get the tag <name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> , but the additional attributes may cause problem to the client.

As I have read, semantically it does make sense to represent the null values this way (using @XmlElement(nillable=true) ).

    package com.mns.mnsutilities.jaxb.model;

    import java.util.ArrayList;
    import java.util.List;

    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlElementWrapper;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    import javax.xml.bind.annotation.XmlType;


    @XmlRootElement(name="Emp_MNS")
    @XmlType(propOrder= {"name", "age", "role", "gender", "addressesList"})
    public class Employee {
        private int id;
        private String gender;
        private int age;
        private String name;
        private String role;
        private String password;
        private List<Address> addressesList;

        public Employee() {}

        public Employee(int id, String gender, int age, String name, String role,
                String password) {
            super();
            this.id = id;
            this.gender = gender;
            this.age = age;
            this.name = name;
            this.role = role;
            this.password = password;
        }

        public Employee(int id, String gender, int age, String name, String role,
                String password, List<Address> addressesList) {
            super();
            this.id = id;
            this.gender = gender;
            this.age = age;
            this.name = name;
            this.role = role;
            this.password = password;
            this.addressesList = addressesList;
        }

        @XmlAttribute
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        @XmlElement(name = "gen", nillable=true)
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @XmlElement(nillable=true)
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getRole() {
            return role;
        }
        public void setRole(String role) {
            this.role = role;
        }
        @XmlTransient
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        @XmlElementWrapper( name="addresses" )
        @XmlElement(name = "address")
        public List<Address> getAddressesList() {
            if(addressesList == null){
                addressesList = new ArrayList<>();
            }
            return addressesList;
        }

        public void setAddressesList(List<Address> addressesList) {
            this.addressesList = addressesList;
        }

        @Override
        public String toString() {
            return "Employee [id=" + id + ", gender=" + gender + ", age=" + age
                    + ", name=" + name + ", role=" + role + ", password="
                    + password + ", addressesList=" + addressesList + "]";
        }
    }

Expanding on the comment made by Ian Roberts you could leverage field access and have the property treat a field value of "" as null.

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

    private String name = "";

    public String getName() {
        if(name.length() == 0) {
            return null;
        }
        return name;
    }

    public void setName(String name) {
        if(null == name) {
            this.name = "";
        } else {
            this.name = name;
        }
    }

i got the same problem,But my field is enum,It can't be emyty string;so I just use

myXml.replaceAll("xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"","")

in another solution,I add a empty string value to my enum,such as

@XmlEnum
@Getter
public enum MyEnum {


    @XmlEnumValue("1") Apple("1"),
    @XmlEnumValue("2") Banner("2"),
    @XmlEnumValue("") NULL("3");

    MyEnum(String value, String description) {
        this.value = value;
        this.description = description;
    }

    private String value;

    private String description;
}

so that I can set MyEnum.NULL to filed;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM