java-JAXB解组对嵌套对象始终为null值
作者:互联网
我有一个Web服务,通过编写其WSDL并在XSD底下进行定义,并且Java服务器代码类/ java绑定是使用JAXB / xjc生成的.
一切看起来都很好,服务可以正常运行…但是对于每个请求(在查看日志输出时在接收后看起来格式正确)在通过我的Java代码访问时,嵌套元素似乎始终为null.
有人可以弄清楚为什么customerId.getCustomer()总是返回null吗?
我的XSD(部分):
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns:tip="http://example.org/tip" xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.org/tip/pro">
<complexType name="id">
<attribute name="id" type="int" use="required"/>
<attribute name="name" type="string" use="optional"/>
</complexType>
<complexType name="customer_id">
<sequence>
<element name="customer" type="tip:id" minOccurs="0"/>
</sequence>
</complexType>
<element name="get_customer_request" type="tip:customer_id"/>
</schema>
生成的类CustomerId:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer_id", propOrder = {"customer"})
public class CustomerId {
protected Id customer;
public Id getCustomer() {
return customer;
}
public void setCustomer(Id value) {
this.customer = value;
}
}
为Id生成的类看起来类似,我认为没有什么特别的.
在我的请求处理程序中,我得到了以下摘录:
处理程序:
JAXBElement<?> request = requestHandler.unmarshallRequest(inputStream);
Object jaxbClass = request.getDeclaredType();
expectedClass = CustomerId.class;
// next line does not throw exception with given XML
if (jaxbClass != expectedClass) throw new IllegalArgumentException();
CustomerId customerId = (CustomerId)request.getValue();
if (customerId == null) {
logInfo("customerId: null");
} else if (customerId.getCustomer() == null) {
// this is the part that always will be executed... why?
logInfo("customerId.customer: null");
} else {
logInfo("customer id: " + customerId.getCustomer().getId());
// return mbean.getCustomer(customerId);
}
最后是一个示例请求XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<m:get_customer_request xmlns:m="http://example.org/tip/pro">
<customer id="0" name="help"/>
</m:get_customer_request>
我删除了SOAP信封和正文标签,因为这不会引起任何麻烦.
有人看到我在做什么错吗? (我很确定,我知道…)
感谢您的努力!
解决方法:
第1部分
When I create a new Id and set customerId.customer with this, the full
output is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<get_customer_request xmlns="example.com/tip/pro">
<customer name="xy" id="1"/>
</get_customer_request>
根据此信息,您的JAXB映射似乎希望customer元素位于example.com/tip/pro命名空间中,并且您的请求文档应为:
<?xml version="1.0" encoding="ISO-8859-1"?>
<m:get_customer_request xmlns:m="http://example.org/tip/pro">
<m:customer id="0" name="help"/>
</m:get_customer_request>
第2部分
When putting m: prefix to customer element in my request, the parser
complains that he found m:customer and expected customer.
这意味着您的XML模式与您的映射不匹配.如果希望customer元素位于名称空间中,则可以将XML模式更改为以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns:tip="http://example.org/tip"
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/tip/pro"
elementFormDefault="qualified">
...
</schema>
有关JAXB和名称空间的更多信息,请参见:
> http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
标签:unmarshalling,jaxb,web-services,xml,java 来源: https://codeday.me/bug/20191127/2075416.html