这是我的问题:我需要XML中存在by object的名称标签,但没有nillable属性:总之就像<name/>
。这是该对象的代码。
如果name为null,我会得到标签<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
,但是其他属性可能会对客户端造成问题。
如我所读,从语义上讲,以这种方式(使用@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 + "]";
}
}
最佳答案
扩展Ian Roberts的评论,您可以利用字段访问并将属性将""
的字段值视为空。
@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;
}
}
关于java - JAXB-防止可设置属性xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance” xsi:nil =“true”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23085787/