如何配置jaxb setter?

时间:2013-07-27 06:31:09

标签: java xml jaxb marshalling

我解析一个xml文件,并希望有两个整数属性的setter:

@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "")
@XmlRootElement(name = "request")
public class Request {
    private Integer intTag;

    @XmlElement(required = true, name = "intTag", nillable = true)
    public Integer getIntTag () {
        return intTag;
    }

    @XmlElement(required = true, name = "intTag", nillable = true)
    public void setIntTag (String intTag) {
        if (StringUtils.isBlank(intTag)) {
            this.intTag = null;
        } else {
            this.intTag = Integer.parseInt(intTag);
        }
    }

    public void setIntTag (Integer intTag) {
        this.intTag = intTag;
    }
}

我注释了setIntTag(String),但是Jaxb marchaller调用了setIntTag(Integer)并将Integer传递给它,如果我删除setIntTag(Integer),marchaller会调用字符串setter。

如何注释这些方法以保留两个setter,并告诉marshaller使用字符串setter?

1 个答案:

答案 0 :(得分:1)

我认为这是一种预期的行为。

JAXB将通过Java Beans约定

处理这两种方法
T getXxx();
void setXxx(T xxx);

如果您想none nil but empty字符串为nil,则可以XmlAdapter

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "request")
public class Request {

    private static class EmtpyStringAdapter
        extends XmlAdapter<String, Integer> {

        @Override
        public Integer unmarshal(final String value) throws Exception {
            if (value == null || value.isEmpty()) {
                return null;
            }
            return Integer.valueOf(value);
        }

        @Override
        public String marshal(final Integer bound) throws Exception {
            if (bound == null) {
                return null;
            }
            return bound.toString();
        }
    }

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

        final JAXBContext context = JAXBContext.newInstance(Request.class);

        {
            final Request request1 = new Request();
            request1.setIntTag(0);

            final ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            final Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                                   Boolean.TRUE);
            marshaller.marshal(request1, System.out);
            marshaller.marshal(request1, buffer);

            final Unmarshaller unamrshaller = context.createUnmarshaller();
            final Request request2 = (Request) unamrshaller.unmarshal(
                new ByteArrayInputStream(buffer.toByteArray()));
            System.out.println(request2.getIntTag());
        }

        {
            final Request request1 = new Request();
            request1.setIntTag(null);

            final ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            final Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                                   Boolean.TRUE);
            marshaller.marshal(request1, System.out);
            marshaller.marshal(request1, buffer);

            final Unmarshaller unamrshaller = context.createUnmarshaller();
            final Request request2 = (Request) unamrshaller.unmarshal(
                new ByteArrayInputStream(buffer.toByteArray()));
            System.out.println(request2.getIntTag());
        }

        {
            final String xml = "<request><intTag></intTag></request>";
            final Unmarshaller unamrshaller = context.createUnmarshaller();
            final Request request2 =
                (Request) unamrshaller.unmarshal(new StringReader(xml));
            System.out.println(request2.getIntTag());
        }
    }

    @XmlElement(nillable = true, required = true)
    @XmlJavaTypeAdapter(EmtpyStringAdapter.class)
    public Integer getIntTag() {
        return intTag;
    }

    public void setIntTag(Integer intTag) {
        this.intTag = intTag;
    }

    private Integer intTag;
}

打印

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request>
    <intTag>0</intTag>
</request>
0

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request>
    <intTag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</request>
null

<request><intTag></intTag></request>
null