4 Stimmen

Wie man JAXB anweist, die Eigenschaften der übergeordneten Klasse zu ignorieren

Ich stehe vor folgenden Problemen mit JAXB: Es sieht so aus, als ob JAXB die Eigenschaften von der tiefsten Kindklasse bis zur übergeordneten Klasse analysiert, und die Kindeigenschaft hat Priorität. Ich würde dieses Verhalten gerne irgendwie ändern. Insbesondere:

Die Kinderklasse:

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;
    }
}

Die übergeordnete Klasse:

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);
    }
}

Das Marshalling funktioniert gut:

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);

Output:

<?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>

Die Eigenschaft, die sich in der untergeordneten Ebene befindet, wird jedoch ignoriert. BasicDocument Klasse ( t.xml ist genau die obige XML):

JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);

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

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

Output:

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

Erwartet:

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

Grundsätzlich @XmlAccessorType(XmlAccessType.NONE) auf BasicDocument (siehe Ignorieren einer übergeordneten Klasse bei der Serialisierung nach XML ) hatte keine Auswirkungen. Auch das Erstellen von package-info.java im Paket test.sub (siehe @XmlTransient auf fremde oder externe Superklasse ) wie folgt:

@XmlAccessorType(XmlAccessType.NONE)
package test.sub;

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

hatte keine Auswirkungen. Nur @XmlTransient public class BasicDocument gearbeitet. Idealerweise würde ich gerne keine Anmerkungen zum Kind hinzufügen und dieses Verhalten nur über package-info.java . Wie kann ich das tun?

Getestet auf JDK 1.6.0_27, und zusätzlich mit JAXB 2.2.4-1 Runtime im Klassenpfad.

Ist es eine Funktion oder ein Fehler?

2voto

bdoughan Punkte 144925

Sie müssen nur noch die setComment Methode zum Document Klasse. Ohne sie wird sie als schreibgeschützte Eigenschaft behandelt, obwohl die Methode in einer übergeordneten Klasse existiert.

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

Vollständige Quelle für 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);
    }

}

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X