1

我解析一个 xml 文件并希望有 2 个用于整数属性的设置器:

@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,并告诉 marshaller 使用 string setter?

4

1 回答 1

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
于 2013-07-27T07:09:38.743 回答