Marshallin object with StringBuffer attributes

When marshaling an object via JAXB using the StringBuffer attribute, this attribute becomes empty. I wrote a small program to demonstrate the problem:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class JaxbTest {

    private String valueOne;
    private StringBuffer valueTwo;

    public static void main(String[] args) throws Exception {

        JaxbTest object = new JaxbTest();
        object.setValueOne("12345");
        object.setValueTwo(new StringBuffer("54321"));

        JAXBContext context = JAXBContext.newInstance(JaxbTest.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(object, System.out);
    }

    @XmlElement
    public String getValueOne() {
        return valueOne;
    }

    public void setValueOne(String valueOne) {
        this.valueOne = valueOne;
    }

    @XmlElement
    public StringBuffer getValueTwo() {
        return valueTwo;
    }

    public void setValueTwo(StringBuffer valueTwo) {
        this.valueTwo = valueTwo;
    }
}

The output is as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><jaxbTest><valueOne>12345</valueOne><valueTwo/></jaxbTest>

Does anyone know why "valueTwo" is marshaling incorrectly? BTW, I am using java 1.6.0_22.

Thanks in advance!

+3
source share
4 answers

I would recommend using the JAXB XmlAdapter for this use case:

+3
source

JaxB probably doesn't know how to serialize StringBuffer. What I would do to solve such problems is to have a couple of getters / setters:

  • ,

  • @XmlElement

    import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement JaxbTest {

private String valueOne;
private StringBuffer valueTwo;

public static void main(String[] args) throws Exception {

    JaxbTest object = new JaxbTest();
    object.setValueOne("12345");
    object.setValueTwo(new StringBuffer("54321"));

    JAXBContext context = JAXBContext.newInstance(JaxbTest.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(object, System.out);
}

@XmlElement
public String getValueOne() {
    return valueOne;
}

public void setValueOne(String valueOne) {
    this.valueOne = valueOne;
}

public StringBuffer getValueTwo() {
    return valueTwo;
}

public void setValueTwo(StringBuffer valueTwo) {
    this.valueTwo = valueTwo;
}

@XmlElement
public String getValueTwoString() {
    return valueTwo!=null?valueTwo.toString():null;
}

public void setValueTwoString(String valueTwo) {
    this.valueTwo = new StringBuffer(valueTwo);
}

}

, , @XmlElement (name= "valueTwo" ) getValueTwoString(), , .

+2

, getter (, , setter), . @XmlIgnore . :

@XmlRootElement
public class JaxbTest {

    private String valueOne;
    private StringBuffer valueTwo;

    public static void main(String[] args) throws Exception {

        JaxbTest object = new JaxbTest();
        object.setValueOne("12345");
        object.setValueTwo(new StringBuffer("54321"));

        JAXBContext context = JAXBContext.newInstance(JaxbTest.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(object, System.out);
    }

    @XmlElement
    public String getValueOne() {
        return valueOne;
    }

    public void setValueOne(String valueOne) {
        this.valueOne = valueOne;
    }

    @XmlIgnore
    public StringBuffer getValueTwo() {
        return valueTwo;
    }

    public void setValueTwo(StringBuffer valueTwo) {
        this.valueTwo = valueTwo;
    }

    @XmlElement(name="valueTwo")
    public String getValueTwoString() {
        return valueTwo.toString();
    }

    public void setValueTwoString(String valueTwo) {
        this.valueTwo = new StringBuffer(valueTwo);
    }
}
0

!

I had this problem when using the servicemix-exec component in ServiceMix 4.2, which is called by this ExecResponse class . It uses StringBuffer for the attributes "outputData" and "errorData".

0
source

Source: https://habr.com/ru/post/1794186/


All Articles