- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的 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
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/
以下代码有效: private List custom; @XmlAnyElement public List getCustom() { if (custom == null) {
我有一个使用 Spring (v4.0.5) 的 http 服务。它的 http 端点是使用 Spring Web MVC 配置的。响应是根据模式生成的 JAXB2 注释类。响应被包装在 JAXBEl
我有一个 xsd,其中包含替换组。当我尝试使用 cxf wsdl2java JAXB 生成 Java 类时,生成 List> 而不是 List 。如何避免 JAXBElement?我尝试使用 bind
我想从 XSD 文件生成带有 JAXB 的 Java 类。 问题是,我总是得到一些像这样的类(删除了命名空间): public static class Action { @X
我在用: GSON 2.3.1 apache-cxf-3.1.1 JBoss EAP 6.4 我正在使用 wsdl2java 从 wsdl 创建 webservice 客户端。 我用以下方法生成类:
我有一个完全可用且经过测试的 REST API。我正在使用 enunciate 创建文档。 请求和响应对象是使用 jaxb 从 xsd 文件生成的。在文档中,可以识别响应对象,但我的请求正文类型是(自
我想实现一个返回 JAXBElement 的方法,下面是代码 @XmlRootElement(name = "history") @XmlAccessorType(XmlAccessType.FIEL
我有多个根元素,因此,我需要写 JAXBElement jaxbBookType = objectFactory.createBookType (bookType); JAXBElement jaxb
考虑 JAXB 生成的以下代码: public class CnpOnlineResponse { protected JAXBElement transactionResponse;
我有以下代码, JAXBContext jc = JAXBContext.newInstance(TestResults.class); Marshaller m = jc.createMarshal
当我尝试从 wsdl 文档生成客户端时,我得到的客户端似乎有很多 JAXBElement 属性,例如 protected List> nameOrLinkingNameOrFamilyName; 我使
我正在使用 NetBeans 和 Glassfish 从现有 WSDL 构建 Web 服务。NetBeans 已从给定的 WSDL 创建了所需的类。WSDL 定义了一些基本数据类型(例如 BaseTy
我定义了一个 Book 类,我想创建一个 JAXBElement 对象,该对象将包含与来自 String 对象的 XML 对应的信息。 例如,我可以有这样的东西: String code = "Har
如何编码 JAXBElement 列表? 例如,我有一个无法注释的 POJO: public class APojo { private String aString; public APojo()
我有一个方法 public Response xyz ( JAXBElement request ) { ...... } 两个不同的地方使用不同的 JAXBElement 调用它。一与 xyz(JA
您好,我想将 JAXBelement 值存储到 String 变量,请告诉我该怎么做。我有以下方法。 public JAXBElement getSessionId() { return s
我已经从我的 XML .xsd 生成了类,并且正在尝试设置一个字段 EndpointID在类(class)内MeterSessionInputRF .我遇到的问题是 setEndpointID方法只接
我正在学习 JAX-WS,并且还介绍了 JAXB。我可以用它来将 java(xml) 编码(un)编码为 xml(java)。但是这行代码的目的是什么,然后我使用 Apache CXF 生成了 WS
我有一个关于 JAXB 的简单问题。这是示例代码: //setter that has input JAXBElement b.setBIC(JAXBElement value); 如何初
我有一个名为 MYClass 的类,其代码如下所示 package com.rest; public class MyClass { private String var; publi
我是一名优秀的程序员,十分优秀!