我想解組一個Java對象的XML響應,儘管解組通過罰款,但Java對象的屬性設置爲null。jaxb unmarshalling給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]
凡在你的XML'命名空間定義爲'的xmlns ams'前綴:AMS =「http://wso2.org/apimanager /安全「'?它必須要麼高於fault元素,要麼至少在你的響應第一行中,比如'' –
Vadim