提供空属性的java jaxb解组
我试图将XML响应解组到Java对象,尽管解组过程很顺利,但Java对象的属性设置为null
以下是回应:
<ams:fault>
<ams:code>900905</ams:code>
<ams:message>Incorrect Access Token Type is provided</ams:message>
<ams:description>Access failure for API: /stockquote, version: 1.0.0 with
key: lI2XVmmRJ9_B_rbh1rwV7Pg3Pp8a</ams:description>
</ams:fault>
下面是我尝试捕获解组内容的Java类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "fault", namespace = "http://wso2.org/apimanager/security")
public class SMSAuthFault
{
@XmlElement(name = "code")
private String code;
@XmlElement(name = "message")
private String message;
@XmlElement(name = "description")
private String description;
public String getCode()
{
return code;
}
public void setCode(String code)
{
this.code = code;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
@Override
public String toString()
{
return "SMSAuthFault [code=" + code + ", message=" + message
+ ", description=" + description + "]";
}
}
以下是我如何试图解开这个回答:
JAXBContext jaxb = JAXBContext.newInstance(SMSAuthFault.class);
Unmarshaller unmarshaller = jaxb.createUnmarshaller();
System.out.println(unmarshaller.unmarshal(new StringReader(m)));
针对上述尝试打印的响应:
SMSAuthFault [code=null, message=null, description=null]
# 1 楼答案
请尝试以下代码:
我的主要班级:
我的输出:
# 2 楼答案
来自javadoc的解组器 https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Unmarshaller.html
使用javax从StringBuffer解组。xml。使改变流动资料来源:
尝试使用StreamSource对象包装StringReader