java-JAXB解组对嵌套对象始终为null值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-JAXB解组对嵌套对象始终为null值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3158字,纯文字阅读大概需要5分钟。
内容图文
我有一个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
内容总结
以上是互联网集市为您收集整理的java-JAXB解组对嵌套对象始终为null值全部内容,希望文章能够帮你解决java-JAXB解组对嵌套对象始终为null值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。