Java JAXB解组器对于嵌套对象始终具有空值
我有一个web服务,它是通过编写WSDL和底层XSD定义的,java服务器代码类/java绑定是使用JAXB/xjc生成的 一切看起来都很好,服务运行正常。。。但是对于每一个请求(当查看日志输出时,在接收后看起来格式良好),当通过java代码访问时,嵌套元素似乎总是空的 有人能理解为什么customerId.getCustomer()总是返回null吗 我的XSD(部分):Java JAXB解组器对于嵌套对象始终具有空值,java,xml,web-services,jaxb,unmarshalling,Java,Xml,Web Services,Jaxb,Unmarshalling,我有一个web服务,它是通过编写WSDL和底层XSD定义的,java服务器代码类/java绑定是使用JAXB/xjc生成的 一切看起来都很好,服务运行正常。。。但是对于每一个请求(当查看日志输出时,在接收后看起来格式良好),当通过java代码访问时,嵌套元素似乎总是空的 有人能理解为什么customerId.getCustomer()总是返回null吗 我的XSD(部分): 生成的类CustomerId: @xmlacessortype(xmlacesstype.FIELD) @XmlTyp
生成的类CustomerId:
@xmlacessortype(xmlacesstype.FIELD)
@XmlType(name=“customer\u id”,proporter={“customer”})
公共类客户ID{
受保护的身份证客户;
公共Id getCustomer(){
退货客户;
}
公共无效设置客户(Id值){
这就是顾客=价值;
}
}
为Id生成的类看起来很相似,我不认为有什么特别的。
在我的请求处理程序中,我得到了以下摘录:
处理程序:
JAXBElement-request=requestHandler.unmarshallRequest(inputStream);
对象jaxbClass=request.getDeclaredType();
expectedClass=CustomerId.class;
//下一行不会对给定的XML引发异常
如果(jaxbClass!=expectedClass)抛出新的IllegalArgumentException();
CustomerId CustomerId=(CustomerId)request.getValue();
if(customerId==null){
logInfo(“customerId:null”);
}else if(customerId.getCustomer()==null){
//这是将永远被执行的部分…为什么?
logInfo(“customerId.customer:null”);
}否则{
logInfo(“客户id:+customerId.getCustomer().getId());
//返回mbean.getCustomer(customerId);
}
最后是一个示例请求XML:
我去掉了肥皂信封和身体标签,因为这不会引起任何麻烦。
谁能看出我做错了什么?(我很确定,我知道……)
谢谢你的夸奖 第一部分 当我创建一个新Id并设置customerId.customer时,完整的 输出为
第二部分 在请求中将m:prefix放入customer元素时,解析器 投诉他找到了m:客户和预期客户 这意味着您的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和名称空间的更多信息,请参阅:
<?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>
<?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>