java - JAXB (MOXy) - 解组空值
问题描述
我在使用 JAXB MOXy 解组 JSON 时遇到问题。
下面是我要解析的 JSON:
{
"accounts": [{
"description": "A",
"balance": 1000,
"balanceAvailable": 1000
},
{
"description": "B",
"balance": 1001,
"balanceAvailable": 1001
},
{
"description": "C",
"balance": 1002,
"balanceAvailable": 1002
}],
"cardPermissions": null,
"totalBalanceAvailable": 1046.19
}
这里的问题在于字段“ cardPermissions
”。
我正在尝试解组一个没有“ cardPermissions
”字段(没有 @XmlElement 注释和类属性)的对象。
JAXB 在解组此字符串时抛出 NullPointerException,因为它是:
"cardPermissions": null
并不是:
"cardPermissions": "null"
要接受空值,我必须在我的 POJO 中添加字段 cardPermissions(带有 @XmlElement 注释)。
这样做,同样没有 getter 和 setter,JAXB 可以正确地解组提供的 JSON。
null 和“null”是完全不同的东西,但我不想在我的 POJO 中包含这个字段,我必须忽略这些 null 值。
编辑
如果我像这样包含根元素(“ customerInfo
”):
{
"customerInfo": {
"accounts": [{
"description": "B",
"balance": 1000,
"balanceAvailable": 1000
},
{
"description": "C",
"balance": 1001,
"balanceAvailable": 1001
},
{
"description": "D",
"balance": 1002,
"balanceAvailable": 1002
}],
"cardPermissions": null,
"totalBalanceAvailable": 1046.19
}
}
JSON被正确解析,但我不想在解组json时包含root。
这是我的 POJO:
@XmlRootElement(name = "customerInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomerInfo {
@XmlElement(name = "name")
private String firstName;
@XmlElement(name = "surname")
private String lastName;
@XmlElement
private String customerId;
@XmlElement
private String email;
@XmlElement
private String cardPermissions; //This field has no getter and setter. I need it in order to parse correctly the JSON without the root node "customerInfo"
@XmlElement
private List<Account> accounts;
public CustomerInfo() {
}
public CustomerInfo(String firstName, String lastName, String emailAddress) {
this.firstName = firstName;
this.lastName = lastName;
this.email = emailAddress;
}
public String getFullName() {
return firstName + " " + lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
}
解决方案
你有两个选择:
- 在注解中设置属性
required
为false
@XmlElement
name
为所有其他字段的注释中的属性设置一个值@XmlElement
,然后删除不需要的字段。通过这样做,该字段将被忽略。
有关更多和好的细节,请访问这个旧帖子。
推荐阅读
- html - 正方形 500x500 像素图像在大约 125 像素的四周具有透明背景。有什么方法可以使用 CSS 进行裁剪?
- python - python中打印函数的“end”参数上的vscode错误
- asp.net - 无法在 Web 服务器上开始调试。远程服务器返回错误:(403) Forbidden
- mysql - 如何在没有forloop的情况下插入一批空行?
- javascript - 为什么我的导航栏和我的页面两侧之间有一点间隙?
- python-3.x - 将模块导入 ipython 时遇到问题
- oauth - 使用 Azure AD 身份验证访问本地 SAML/OAuth 保护的 SAP API(ADFS 联合)
- html - 将两个包含“过滤器”的 CSS 类应用于 HTML 元素
- java - Apache Beam 中嵌套的 Top N
- python - 表单未在 django admin 中保存电子邮件