让我们说我上课了
@XmlType(propOrder = { "one","two" }) @XmlRootElement(name = "search") public class Search { protected One one; protected Two two; //getters & setters }
我想要一个扩展这个类的类
例如
@XmlType(propOrder = { "three" }) @XmlRootElement(name = "searchExtended") public class SearchExtended extends Search { protected Three three; //getters & setters }
你如何正确地声明propOrder,我之前尝试了这个,它没有使用我认为它会的顺序.这是如何通过注释处理的? /你应该如何在扩展类中声明这一点?
解决方法
父项属性将根据子属性之前的指定顺序进行编组.如果使用@XmlTransient注释父类,则可以在子类的propOrder中包含父类的属性.
> http://blog.bdoughan.com/2012/08/jaxbs-xmltransient-and-property-order.html
UPDATE
Is there a way i can make it transistant but still use it normally?
不,在类上设置@XmlTransient会将其从JAXB认为映射的类中删除. JAXB在子类的属性与XML模式的规则匹配之前对超类的属性进行分析的原因.如果您的Search类未标记@XmlTransient,则相应的XML架构如下所示.根据XML模式规则,为了使searchExtended类型的元素有效,超类型的元素必须在子类型中定义的任何元素之前发生.
<xs:complexType name="searchExtended"> <xs:complexContent> <xs:extension base="search"> <xs:sequence> <xs:element name="three" type="three" minOccurs="0"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="search"> <xs:sequence> <xs:element name="one" type="one" minOccurs="0"/> <xs:element name="two" type="two" minOccurs="0"/> </xs:sequence> </xs:complexType>
您可以通过运行以下代码来查看与JAXB模型对应的XML架构:
import java.io.IOException; import javax.xml.bind.*; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(SearchExtended.class); jc.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri,String suggestedFileName) throws IOException { StreamResult result = new StreamResult(System.out); result.setSystemId(suggestedFileName); return result; } }); } }