JAXBElement.getValue()返回null [英] JAXBElement.getValue() is returning null
问题描述
我的Pojo课程中有一对多的映射。
商店有分店,分店有很多商店
这是商店代码:
I have one to many mapping in my Pojo classes. A shop has a branch and a branch has many shops Here's Shop's Code:
@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
}
我正在发布一个web服务一些基本方法。我的wsimport生成如下所示的Branch类
I'm publishing a webservice with some basic methods. and my wsimport is generating Branch class as below
@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
}
我不知道它为什么是列表与LT;&的JAXBElement LT;对象>>而不是List< JAXBElement< Shop>>。但无论如何。我有一个方法,它返回所有分支,并且工作正常。当我从branch的实例中提取branchShop时,我获得了branchShop列表的正确大小,但是对于list中的所有项,getValue都返回NULL。
以下是简要代码:
I have no idea why it is List<JAXBElement<Object>> and not List<JAXBElement<Shop>>. But anyway. I have a method which returns all branches and that is working fine. When i extract branchShop from branch's instance i'm getting correct size for branchShop list but for all of the items in list getValue is returning NULL. Below is brief code:
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
******************Branch:KukatPalli Steer 2 Kukatpalli****************** javax.xml.bind.JAXBElement@45d9d7beValueclass com.pencilhouse.webservices.Branch null
生成的WSDL非常大。我只发布分店和商店的类型。我正在使用Endpoint发布webservice
The WSDL generated is quite large. I'm posting only type of Branch and Shop. I'm publishing webservice using Endpoint
生成的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>
截获的信息:
请求:
Intercepted Information: Request:
<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>
推荐答案
@XmlIDREF
提供了一种指定文档内引用的方法。需要通过单独的嵌套关系(例如 @XmlElement
)重新呈现每个对象,以将数据导入XML文档。
@XmlIDREF
provides a way to specify intra-document references. It is required that each object be refrerenced through a sepearate nested relationship (such as @XmlElement
) to get the data into the XML document.
我在博客上写了更多关于 @XmlIDREF
的信息:
I have written more about @XmlIDREF
on my blog:
- http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html
我看到你的博客,我在我的xml响应中获取数据,但问题是
当我使用wsimport在客户端生成我的类时,它是
为@XmlIDRef生成类型Object,并且它没有像我在我的问题中解释的那样为这些
属性设置数据。
I saw your blog, I'm getting data in my xml response but the issue is that when i generate my classes on client side using wsimport it is generating type Object for @XmlIDRef and it doesn't set data for these properties as i explained in my question.
在你的例子中,你要去:classes - > schema - > classes。由于Java类和XML Schema不是完美匹配,并且JAXB没有在生成的模式中放置任何特定于JAXB的元数据,因此某些信息会丢失。您可以按如下方式解决此问题:
In your example you are going: classes -> schema -> classes. Since Java classes and XML Schema are not a perfect match, and JAXB does not put any JAXB specific metadata in the generated schema some information is lost. You can fix this as follows:
您可以使用外部绑定文件键入IDREF属性。
You can use an external bindings file to type the IDREF property.
<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
<jaxb:bindings node="xsd:complexType[@name='branch']//xsd:element[@name='BranchShop']">
<jaxb:property>
<jaxb:baseType name="org.example.Shop"/>
</jaxb:property>
</jaxb:bindings>
</jaxb:bindings>
这篇关于JAXBElement.getValue()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!