gpt4 book ai didi

java - JAXB - 防止可空属性 xmlns :xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:nil ="true"

转载 作者:行者123 更新时间:2023-11-30 03:56:20 26 4
gpt4 key购买 nike

这是我的问题:我需要在 XML 中显示 by 对象的名称标签,但没有可空属性:简而言之,如 <name/> 。这是该对象的代码。如果 name 为空,我会得到标签 <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 的评论,您可以利用字段访问并使属性将 "" 字段值视为 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;
}
}

关于java - JAXB - 防止可空属性 xmlns :xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:nil ="true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23085787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com