我有一个web服务,通过编写其WSDL和基础XSD来定义它,并且使用JAXB / xjc生成了Java服务器代码类/ Java绑定。
一切看起来都很好,服务正在正常运行...但是对于每个请求(在接收时看起来格式良好),当通过我的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信封和主体标签,因为这不会引起任何问题。 有人能看出我做错了什么吗?(我很确定我做错了...) 感谢你的努力!
customer
元素位于example.com/tip/pro
命名空间中。这意味着在你的请求中它应该有m
前缀。 - bdoughan