我的项目目前有这个环境:
public abstract class Foo {
private List<Thing> things;
public List<Thing> getThings() { return this.things; }
}
public abstract class Bar extends Foo {
@XmlElements({@XmlElement(name = "first", type = First.class)})
public List<Thing> getThings() { return super.getThings(); }
}
public class Bobar extends Bar {
@XmlElements({@XmlElement(name = "second", type = Second.class)})
public List<Thing> getThings() { return super.getThings(); }
}
对于下面的XML文档
<bobar>
<first>blablabla</first>
<second>blublublu</second>
</bobar>
当我这样做
context = JAXBContext.newInstance("the.package.structure");
unmarshaller = context.createUnmarshaller();
Bar object = (Bar) unmarshaller.unmarshal("path-to-xml-document");
Bar object 在集合中只有一个元素,没有 2 个。First 元素完全丢失,当我尝试执行 object.getThings( ),它的大小为 1,集合中唯一的对象是 Second 的实例。有人可以帮助我如何获得集合中的两个对象吗?如果那不可能,我怎样才能实现类似的目标?
我这样做的原因是(在我的项目逻辑中)每个 Bobar 的东西集合在其集合中都有一个 First,但不是每个 Bar 在其集合中有一个 Second,Foo 是一个泛型类。
当我更改 XML 文档中的顺序时,输出不同。
<bobar>
<second>blablabla</second>
<first>blublublu</first>
</bobar>
在这种情况下,我只获得了集合中 First 的一个实例,Second 丢失了。并且更多地改变场景,我得到了有趣的结果:
public abstract class Foo {
private List<Thing> things;
public List<Thing> getThings() { return this.things; }
}
public abstract class Bar extends Foo {
@XmlElements({@XmlElement(name = "first", type = First.class), @XmlElement(name = "third, type = Third.class)})
public List<Thing> getThings() { return super.getThings(); }
}
public class Bobar extends Bar {
@XmlElements({@XmlElement(name = "second", type = Second.class)})
public List<Thing> getThings() { return super.getThings(); }
}
如果我这样做
<bobar>
<third>bliblibli</third>
<second>blablabla</second>
<first>blublublu</first>
</bobar>
理论上,我认为这不应该针对由此生成的 XML 模式进行验证,因为这里的顺序不正确。但除此之外,在这种情况下,我得到了 Second 和 First,Third 丢失了。
最佳答案
无法在父类(super class)型上注释属性,并让子尝试增量添加到该映射。以下是您可以支持您所追求的所有用例的一种方式。需要注意的一件事是对象层次结构中的所有级别都将支持同一组元素。您需要使用外部验证方式来限制所需的值。
如果 Thing 是类而不是接口(interface),并且 First 和 Second 扩展 Thing 那么你可能是对使用 @XmlElementRef 而不是 @XmlElements 感兴趣(参见:http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html)。它将以一些验证为代价为您提供更大的灵 active (很难限制有效值的集合)。
栏
我们将使用 @XmlTransient 注释 Bar,这样 JAXB 实现就不会处理它。
package forum11698160;
import java.util.List;
import javax.xml.bind.annotation.XmlTransient;
@XmlTransient
public abstract class Bar extends Foo {
public List<Thing> getThings() {
return super.getThings();
}
}
波巴尔
@XmlElementRef 对应于 XML 模式中的替换组的概念。与属性匹配的值将基于 @XmlRootElement 声明。
package forum11698160;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Bobar extends Bar {
@XmlElementRef
public List<Thing> getThings() {
return super.getThings();
}
}
东西
由于 JAXB 实现无法使用反射来查找类型的所有子类,我们可以使用 @XmlSeeAlso 注释来帮忙。如果您不使用此注释,则在引导 JAXBContext 时需要包含所有子类型。
package forum11698160;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlSeeAlso({First.class, Second.class})
public class Thing {
}
首先
我们需要用@XmlRootElement注释First:
package forum11698160;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class First extends Thing {
}
第二
Second 也需要用 @XmlRootElement 注释:
package forum11698160;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Second extends Thing {
}
演示
package forum11698160;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Bobar.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11698160/input.xml");
Bobar bobar = (Bobar) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(bobar, System.out);
}
}
input.xml/输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bobar>
<first/>
<second/>
</bobar>
其他文件
以下是运行此示例所需的其他文件:
福
package forum11698160;
import java.util.*;
public abstract class Foo {
private List<Thing> things = new ArrayList<Thing>();
public List<Thing> getThings() {
return this.things;
}
}
关于java - JAXB 继承冲突 - 在子类上重新注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11698160/
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/