- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正面临一个编码/解码问题,涉及使用 MOXy 的 JAXB 实现和外部元数据绑定(bind)文件的继承和多态性。
我无法控制 XML 文件或模型类。
模型内部有多个类继承其他 DTO 类。
这是我正在使用的环境的示例。此示例仅用于某些语法目的,实际环境涉及嵌套继承、集合等:
这是将被继承的类
class A {
private String name;
public String getName(){
return name;
}
public void setName(String value){
name = value;
}
}
class B extends A {
private String attrFromB;
public String getAttrFromB(){
return attrFromB;
}
public void setAttrFromB(String value){
attrFromB = value;
}
}
class C extends A {
private String attrFromC;
public String getAttrFromC(){
return attrFromC;
}
public void setAttrFromC(String value){
attrFromC= value;
}
}
class MyContainerClass{
private A myObject;
public A getMyObject(){
return myObject;
}
public void setMyObject(A value){
myObject = value;
}
}
<MyContainer>
<MyObject nameA="foo" />
</MyContainer>
<MyContainer>
<MyObject nameB="foo" attrFromB="bar" />
</MyContainer>
<MyContainer>
<MyObject nameC="foo" attrFromC="bar" />
</MyContainer>
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="com.test.example"
version="2.1">
<java-type name="A" xml-accessor-type="NONE">
<xml-root-element name="MyObject" />
<java-attributes>
<xml-element java-attribute="name" xml-path="@nameA" />
</java-attributes>
</java-type>
<java-type name="B" xml-accessor-type="NONE">
<xml-root-element name="MyObject" />
<xml-see-also>
com.test.example.A
</xml.see.also>
<java-attributes>
<xml-element java-attribute="name" xml-path="@nameB" />
<xml-element java-attribute="attrFromB" xml-path="@attrFromB" />
</java-attributes>
</java-type>
<java-type name="C" xml-accessor-type="NONE">
<xml-root-element name="MyObject" />
<xml-see-also>
com.test.example.A
</xml.see.also>
<java-attributes>
<xml-element java-attribute="name" xml-path="@nameC" />
<xml-element java-attribute="attrFromC" xml-path="@attrFromC" />
</java-attributes>
</java-type>
<java-type name="MyContainer" xml-accessor-type="NONE">
<xml-root-element name="MyContainer" />
<java-attributes>
<xml-element java-attribute="myObject" type="com.test.example.A" xml-path="MyObject" />
</java-attributes>
</java-type>
</xml-bindings>
[Exception [EclipseLink-44] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class indicator field from database row [UnmarshalRecord()].
--Ignoring attribute [name] on class [com.test.example.B] as no Property was generated for it.
--Ignoring attribute [name] on class [com.test.example.C] as no Property was generated for it.
最佳答案
以下是您的问题的答案。问题 2 的答案也是问题 1 的答案。
1st question : I understand that this is normal, Jaxb needs some way to determine the type of MyContaioner.myObject attribute. The problem is that I have no access to the incoming XML files, so I cant add xsi:type fields to them. Is there a way to determine a class based on the presence of a specific attribute in it ? regardless of it's value. If the source xml contains a @attrFromC attribute, I know the object should be of type C. If it contains attrFromB, it's B.
ClassExtractor
EclipseLink JAXB (MOXy) 中的扩展名对于这个用例:
ClassExtractor
是一些你可以实现的代码来帮助 MOXy 确定它应该实例化哪个类。您通过了
Record
您可以通过 XPath 询问当前元素的属性是否存在,以确定应该实例化哪个类。
package com.test.example;
import org.eclipse.persistence.descriptors.ClassExtractor;
import org.eclipse.persistence.sessions.*;
public class MyClassExtractor extends ClassExtractor{
@Override
public Class<?> extractClassFromRow(Record record, Session session) {
if(null != record.get("@attrFromB")) {
return B.class;
} else if(null != record.get("@attrFromC")) {
return C.class;
} else {
return A.class;
}
}
}
ClassExtractor
使用
@XmlClassExtractor
注解。您也可以通过外部元数据文件执行此操作。我已经修改了您问题中包含的内容以包含以下内容:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="com.test.example"
version="2.3">
<java-types>
<java-type name="A" xml-accessor-type="NONE">
<xml-class-extractor class="com.test.example.MyClassExtractor"/>
<xml-root-element name="MyObject" />
<java-attributes>
<xml-attribute java-attribute="name" name="nameA" />
</java-attributes>
</java-type>
<java-type name="B" xml-accessor-type="NONE">
<xml-root-element name="MyObject" />
<java-attributes>
<xml-attribute java-attribute="name" name="nameB" />
<xml-attribute java-attribute="attrFromB"/>
</java-attributes>
</java-type>
<java-type name="C" xml-accessor-type="NONE">
<xml-root-element name="MyObject" />
<java-attributes>
<xml-attribute java-attribute="name" name="nameC" />
<xml-attribute java-attribute="attrFromC"/>
</java-attributes>
</java-type>
<java-type name="MyContainerClass" xml-accessor-type="NONE">
<xml-root-element name="MyContainer" />
<java-attributes>
<xml-element java-attribute="myObject" name="MyObject" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
myObject
所持有的类型。属性(property):
package com.test.example;
import java.io.StringReader;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "com/test/example/oxm.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {MyContainerClass.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader aXml = new StringReader("<MyContainer><MyObject nameA='foo'/></MyContainer>");
MyContainerClass myContainerA = (MyContainerClass) unmarshaller.unmarshal(aXml);
System.out.println(myContainerA.getMyObject().getClass());
StringReader bXml = new StringReader("<MyContainer><MyObject nameB='foo' attrFromB='bar'/></MyContainer>");
MyContainerClass myContainerB = (MyContainerClass) unmarshaller.unmarshal(bXml);
System.out.println(myContainerB.getMyObject().getClass());
StringReader cXml = new StringReader("<MyContainer><MyObject nameC='foo' attrFromC='bar'/></MyContainer>");
MyContainerClass myContainerC = (MyContainerClass) unmarshaller.unmarshal(cXml);
System.out.println(myContainerC.getMyObject().getClass());
}
}
[EL Warning]: 2012-01-20 10:36:41.828--Ignoring attribute [name] on class [com.test.example.B] as no Property was generated for it.
[EL Warning]: 2012-01-20 10:36:41.828--Ignoring attribute [name] on class [com.test.example.C] as no Property was generated for it.
class com.test.example.A
class com.test.example.B
class com.test.example.C
2nd question : The other problem is that I dont know if Jaxb is capable of overriding xml attribute names like it is expected inside the XML file (@nameA, @nameB and nameC all referring to A.name), is there a way to do it ?
XmlAdapter
对于这个问题。这种方法也可以用来回答你的第一个问题:
package com.test.example;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class AAdapter extends XmlAdapter<AAdapter.AdaptedA, A> {
@Override
public AdaptedA marshal(A a) throws Exception {
if(null == a) {
return null;
}
AdaptedA adaptedA = new AdaptedA();
if(a instanceof C) {
C c = (C) a;
adaptedA.nameC = c.getName();
adaptedA.attrFromC = c.getAttrFromC();
} else if(a instanceof B) {
B b = (B) a;
adaptedA.nameB = b.getName();
adaptedA.attrFromB = b.getAttrFromB();
} else if(a instanceof A) {
adaptedA.nameA = a.getName();
}
return adaptedA;
}
@Override
public A unmarshal(AdaptedA adaptedA) throws Exception {
if(null == adaptedA) {
return null;
}
if(null != adaptedA.attrFromC) {
C c = new C();
c.setName(adaptedA.nameC);
c.setAttrFromC(adaptedA.attrFromC);
return c;
} else if(null != adaptedA.attrFromB) {
B b = new B();
b.setName(adaptedA.nameB);
b.setAttrFromB(adaptedA.attrFromB);
return b;
}
A a = new A();
a.setName(adaptedA.nameA);
return a;
}
public static class AdaptedA {
@XmlAttribute public String nameA;
@XmlAttribute public String nameB;
@XmlAttribute public String nameC;
@XmlAttribute public String attrFromB;
@XmlAttribute public String attrFromC;
}
}
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="com.test.example"
version="2.3">
<java-types>
<java-type name="MyContainerClass" xml-accessor-type="NONE">
<xml-root-element name="MyContainer" />
<java-attributes>
<xml-element java-attribute="myObject" name="MyObject">
<xml-java-type-adapter value="com.test.example.AAdapter"/>
</xml-element>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
package com.test.example;
import java.io.StringReader;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo2 {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "com/test/example/oxm-2.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {MyContainerClass.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringReader aXml = new StringReader("<MyContainer><MyObject nameA='foo'/></MyContainer>");
MyContainerClass myContainerA = (MyContainerClass) unmarshaller.unmarshal(aXml);
System.out.println(myContainerA.getMyObject().getClass());
marshaller.marshal(myContainerA, System.out);
StringReader bXml = new StringReader("<MyContainer><MyObject nameB='foo' attrFromB='bar'/></MyContainer>");
MyContainerClass myContainerB = (MyContainerClass) unmarshaller.unmarshal(bXml);
System.out.println(myContainerB.getMyObject().getClass());
marshaller.marshal(myContainerB, System.out);
StringReader cXml = new StringReader("<MyContainer><MyObject nameC='foo' attrFromC='bar'/></MyContainer>");
MyContainerClass myContainerC = (MyContainerClass) unmarshaller.unmarshal(cXml);
System.out.println(myContainerC.getMyObject().getClass());
marshaller.marshal(myContainerC, System.out);
}
}
class com.test.example.A
<?xml version="1.0" encoding="UTF-8"?>
<MyContainer>
<MyObject nameA="foo"/>
</MyContainer>
class com.test.example.B
<?xml version="1.0" encoding="UTF-8"?>
<MyContainer>
<MyObject nameB="foo" attrFromB="bar"/>
</MyContainer>
class com.test.example.C
<?xml version="1.0" encoding="UTF-8"?>
<MyContainer>
<MyObject nameC="foo" attrFromC="bar"/>
</MyContainer>
关于inheritance - eclipselink/Moxy : inheritance and attribute name oveloading based on type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8942945/
我正在为我的项目测试 FlowJS,我遇到了 Type Aliases 的问题. 我为 API 构建了一个 Web SDK:我有一个使用此方法的 Helper 类: class Helper {
我是 Dart 的新手,仍在学习它带来的所有细微差别。我当前的任务真正困扰的一件事是从父类继承(或代理)静态方法。 void main() { EnglishCasual.greet(); } c
我正在尝试巩固 Go 提供的继承概念(也许是“组合”而不是纯粹的继承)。但是,我无法理解为什么我不能将“父”类型用作 func 参数来生成作用于该参数的通用函数。 package main impor
我在准确表述它时遇到问题,所以我在标题中留下了更笼统的描述(如果您对问题有更准确的描述,请发表评论,我会编辑标题)。 问题: AudioStream 和VideoStream 两个类派生自基类Medi
我的应用程序不仅具有“用户”,而且还具有“管理员”和“ super 管理员”。由于所有这三个共享相同的属性,因此我只想使用一个带有附加属性“role”的表,该属性可以是“user”,“admin”或“
我正在使用数据库优先方法和 DbContext。我的数据模型中有几个继承结构 (TPH)。但是 DbContext 只为基类创建一个 DbSet,而没有为子类创建一个 DbSet。我应该如何检索指定子
我正在使用数据库优先方法和 DbContext。我的数据模型中有几个继承结构 (TPH)。但是 DbContext 只为基类创建一个 DbSet,而没有为子类创建一个 DbSet。我应该如何检索指定子
我有一个生成很多子对象的应用程序,每个子对象都与一些全局应用程序对象一起工作,例如在全局应用程序注册表中注册自己,更新应用程序统计信息等。 应用程序应该如何将访问这些全局对象的能力传递给 child
我有 2 个结构,其中一个继承了由 type Common struct {...} 表示的所有结构中共有的值 type Common struct{ Id int CreatedAt
我的代码: Ext.onReady(function() { // Every property is declared inside the class Ext.define('MyCustomPa
我有一个包含多个类的应用程序,这些类继承/是第三方库类的子类。例如,来自 Qt Framework 的 QDialog。 我应该在应用程序的 UML 类图中指定继承关系吗? 是否期望(或标准)表示此类
我遇到了这个问题,因为我认为我对 Backbone 的理解不正确。 我有一个名为 Runnable 的父类(super class)和一组继承自它的子类: var Runnable = Backbon
考虑以下 HTML: foo bar 和 CSS: a { text-decoration:none; border-bottom-width: 0px; border-bot
我想做个模板Class BaseDialog ,但在进行最终对话时 MyDialog1 , 继承了 BaseDialog , 我收到错误,然后无法在设计模式下显示对话框。 以下是我得到的错误列表。 a
我对 android 文档中的这两个术语感到困惑!! these screenshot about what i mean 最佳答案 常量是用 static 和 final 修饰符声明的字段。继承常量
我最近遇到了一个新的警告: 继承构造函数不继承省略号 我正在尝试管道 Object{42}; // ... into an init that handles integers ...和... Obj
我正在使用 TypeScript 开发 Aurelia 应用程序。在此应用程序中,我定义了一组自定义元素,每个元素共享一组可绑定(bind)属性,这些属性被转换为 css 设置,如以下简化示例所示:
我想知道为什么人们会说: “继承类不继承构造函数”。 如果你可以使用父类的构造函数,无参构造函数无论如何都会被自动调用。 例子: #include using namespace std; clas
我刚刚开始探索 Kotlin 语言。我正在为继承、var&val 和副作用而苦苦挣扎。 如果我用 val x 声明一个特征 A 并在 AImpl 中覆盖 x ,则可以将其覆盖为 var(参见下面的代码
我正面临一个编码/解码问题,涉及使用 MOXy 的 JAXB 实现和外部元数据绑定(bind)文件的继承和多态性。 我无法控制 XML 文件或模型类。 模型内部有多个类继承其他 DTO 类。 这是我正
我是一名优秀的程序员,十分优秀!