【发布时间】:2017-10-16 16:48:00
【问题描述】:
我正在尝试解组对 Java 对象的 XML 响应,尽管解组过程正常,但 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]
【问题讨论】:
-
在您的 xml
ams前缀中为命名空间定义为xmlns:ams="http://wso2.org/apimanager/security"的位置?它必须在高于故障元素或至少在您的响应第一行中,例如<ams:fault xmlns:ams="http://wso2.org/apimanager/security">