gpt4 book ai didi

java - JAXBElement.getValue() 返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:57 24 4
gpt4 key购买 nike

我的 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());
}

对/对

******************Branch:KukatPalli Steer 2 Kukatpalli****************** javax.xml.bind.JAXBElement@45d9d7beValueclass com.pencilhouse.webservices.Branch null

生成的 WSDL 非常大。我只发布 Branch 和 Shop 类型。我正在使用 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>

拦截信息:要求:

<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 的内容:


更新

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.

在您的示例中,您将进行:类 -> 模式 -> 类。由于 Java 类和 XML 模式不是完美匹配,并且 JAXB 没有将任何 JAXB 特定的元数据放入生成的模式中,一些信息丢失了。您可以按如下方式解决此问题:

您可以使用外部绑定(bind)文件来键入 IDREF 属性。

<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>

关于java - JAXBElement.getValue() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com