我的 Pojo 类中有一对多映射。
一家店有一家分店,一个分店有很多家店铺
这是商店代码:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Shop")
public class Shop {
@XmlID
private String name;
@XmlIDREF
@XmlElement(name="ShopBranch",type=Branch.class)
private Branch branch;
//Getter Setter
}
以下是分行代码:
@XmlAccessorType(XmlAccessType.FIELD)
public class Branch {
@XmlID
private String name;
private String address;
@XmlIDREF
@XmlElement(nillable=false,required=true)
private List<Shop> shops;
//Getter and Setters
}
我正在发布一个带有一些基本方法的网络服务。我的 wsimport 正在生成 Branch 类,如下所示
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "branch", propOrder = {
"branchName",
"address",
"branchShop"
})
public class Branch {
@XmlElement(name = "BranchName")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlID
@XmlSchemaType(name = "ID")
protected String branchName;
protected String address;
@XmlElementRef(name = "BranchShop", type = JAXBElement.class)
protected List<JAXBElement<Object>> branchShop;
//Getter-Setter
}
我不知道为什么它是 List> 而不是 List>。但不管怎么说。我有一个返回所有分支的方法,并且工作正常。当我从分支的实例中提取branchShop时,我得到了branchShop列表的正确大小,但对于列表中的所有项目,getValue返回NULL。
下面是简短的代码:
PencilCatalog service= new PencilCatalog();
com.pencilhouse.webservices.PencilService port=service.getPencilCatalogPort();
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, Constant.PENCIL_SERVICE);
List<Branch> branches= port.getAllBranches();
for(Branch b:branches)
{
System.out.println("******************Branch:"+b.getBranchName()+" "+b.getAddress()+"******************");
JAXBElement<Object>o=b.getBranchShop().get(0);
System.out.println(o+"Value"+o.getScope()+" "+o.getValue());
}
o/p
******************分店:KukatPalli Steer 2 Kukatpalli********************
javax.xml.bind.JAXBElement@45d9d7beValueclass
com.pencilhouse.webservices.Branch null
生成的 WSDL 相当大。我只发布分支机构和商店类型。我正在使用 Endpoint 发布 Web 服务
生成的 XML:
<xs:complexType name="shop">
<xs:sequence>
<xs:element name="name" type="xs:ID" minOccurs="0"/>
<xs:element name="ShopBranch" type="xs:IDREF" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="branch">
<xs:sequence>
<xs:element name="BranchName" type="xs:ID" minOccurs="0"/>
<xs:element name="address" type="xs:string" minOccurs="0"/>
<xs:element name="BranchShop" type="xs:IDREF" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
截获的信息:
要求:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getAllBranches xmlns:ns2="PencilServiceHouse"/>
</S:Body>
</S:Envelope>
回复:
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getAllBranchesResponse xmlns:ns2="PencilServiceHouse">
<return>
<name>
KukatPalli
</name>
<address>
Steer 2 Kukatpalli
</address>
<shops>
Pencil World <!-- This is Shop Information which is coming as NULL in java, This is Shop's Name field which is declared as id using @XmlId -->
</shops>
<shops>
Pencils Den
</shops>
<shops>
Pencils Bag
</shops>
</return>
<return>
<name>
Salt Lake
</name>
<address>
Sec V Salt Lake
</address>
<shops>
Pencil World
</shops>
<shops>
Pencils Den
</shops>
</return>
<return>
<name>
Noida
</name>
<address>
Noida Sec 43
</address>
<shops>
Pencils Bag
</shops>
</return>
</ns2:getAllBranchesResponse>
</S:Body>
</S:Envelope>