gpt4 book ai didi

jaxb - 如何告诉 JAXB 忽略来自父类的属性

转载 作者:行者123 更新时间:2023-12-01 04:05:58 30 4
gpt4 key购买 nike

我在使用 JAXB 时面临以下问题:看起来 JAXB 正在分析从最深的子类到父类的属性,并且子属性具有优先级。我想以某种方式改变这种行为。特别是:

子类:

package test.sub;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlTransient;


@XmlAccessorType(XmlAccessType.NONE)
public class BasicDocument {

private String comment;

public String getComment() {
return comment;
}

public void setComment(String cost) {
this.comment = cost;
}
}

父类:
package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import test.sub.BasicDocument;

@XmlRootElement(name="Description", namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#")
@XmlAccessorType(XmlAccessType.PROPERTY)
class Document extends BasicDocument {

private String identifier;

@XmlElement(name = "identifier", namespace = "http://purl.org/dc/terms/")
public String getIdentifier() {
return identifier;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}

@Override
@XmlElement(name = "abstract", namespace = "http://purl.org/dc/terms/")
public String getComment() {
return super.getComment();
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}

编码工作正常:
Document document = new Document();

document.setIdentifier("12A");
document.setComment("special");

StringWriter w = new StringWriter();

jaxbContext.createMarshaller().marshal(document, new StreamResult(w));

System.out.println(w);

输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Description xmlns:ns2="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/dc/terms/">
<abstract>special</abstract>
<identifier>12A</identifier>
</ns2:Description>

但是编码会忽略子项中的属性 BasicDocument类( t.xml 正是上面的 XML):
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);

Document document = (Document) jaxbContext.createUnmarshaller().unmarshal(Document.class.getResourceAsStream("t.xml"));

System.out.println("out: " + document);

输出:
out: Document[identifier=12A,comment=<null>]

预期的:
out: Document[identifier=12A,comment=special]

基本上 @XmlAccessorType(XmlAccessType.NONE)BasicDocument (见 Ignore a parent class when Serializing to XML )没有效果。还在创建 package-info.java包装内 test.sub (见 @XmlTransient on third-party or external super class )像这样:
@XmlAccessorType(XmlAccessType.NONE)
package test.sub;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

没有效果。只有 @XmlTransient public class BasicDocument工作。理想情况下,我不想在 child 上添加任何注释并仅通过 package-info.java 控制此行为.我怎样才能做到这一点?

在 JDK 1.6.0_27 上测试,另外在类路径中使用 JAXB 2.2.4-1 运行时测试。

它是功能还是错误?

最佳答案

您只需要添加 setComment方法到 Document类(class)。没有它,即使该方法存在于父类中,它也会被视为只写属性。

public void setComment(String comment) {
super.setComment(comment);
}

Document的完整来源
package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import test.sub.BasicDocument;

@XmlRootElement(name="Description", namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#")
@XmlAccessorType(XmlAccessType.PROPERTY)
class Document extends BasicDocument {

private String identifier;

@XmlElement(name = "identifier", namespace = "http://purl.org/dc/terms/")
public String getIdentifier() {
return identifier;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}

@Override
@XmlElement(name = "abstract", namespace = "http://purl.org/dc/terms/")
public String getComment() {
return super.getComment();
}

public void setComment(String comment) {
super.setComment(comment);
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}

}

关于jaxb - 如何告诉 JAXB 忽略来自父类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9285370/

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