tangguo

如何使用jax-rs将bean结构映射到其他模式

java

我有这个bean

@XmlRootElement
class Test {
   boolean someValue;
   List<Field> fields;
}

我想序列化为

<fields>
   <field>
       <name>someValue</name>
       <value>...</value>
   </field>
</fields>
<fields>
   <field>
       <name>otherValue</name>
       <value>...</value>
   </field>
</fields>

(或作为json)

我应该如何做,最好使用jaxb注释?

我正在使用运动衫,但答案不必特定于它。


阅读 232

收藏
2020-12-02

共1个答案

小编典典

接下来呢?

使用EclipseLink JAXB(MOXy),您可以执行以下操作。注意:我是MOXy技术负责人。

测试

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlType(propOrder={"someValue", "fields"})
@XmlAccessorType(XmlAccessType.FIELD)
class Test {

    @XmlJavaTypeAdapter(SomeValueAdapter.class)
    @XmlPath("fields[1]")
    boolean someValue;

    @XmlJavaTypeAdapter(FieldsAdapter.class)
    List<Field> fields = new ArrayList<Field>();

    public Boolean isSomeValue() {
        return someValue;
    }

    public void setSomeValue(boolean someValue) {
        this.someValue = someValue;
    }

    public List<Field> getFields() {
        return fields;
    }

    public void setFields(List<Field> fields) {
        this.fields = fields;
    }

    public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
        for(Field field : fields) {
            if("someValue".equals(field.getName())) {
                someValue = Boolean.valueOf(field.getValue());
                fields.remove(field);
            }
        }
    }

}

领域

public class Field {

    private String name;
    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

SomeValueAdapter

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class SomeValueAdapter extends XmlAdapter<AdaptedField, Boolean> {

    @Override
    public Boolean unmarshal(AdaptedField v) throws Exception {
        String value = v.getField().getValue();
        return Boolean.valueOf(value);
    }

    @Override
    public AdaptedField marshal(Boolean v) throws Exception {
        AdaptedField adaptedField = new AdaptedField();
        Field field = new Field();
        field.setName("someValue");
        field.setValue(String.valueOf(v));
        adaptedField.setField(field);
        return adaptedField;
    }

}

字段适配器

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class FieldsAdapter extends XmlAdapter<AdaptedField, Field> {

    @Override
    public Field unmarshal(AdaptedField v) throws Exception {
        return v.getField();
    }

    @Override
    public AdaptedField marshal(Field v) throws Exception {
        AdaptedField adaptedField = new AdaptedField();
        adaptedField.setField(v);
        return adaptedField;
    }

}

AdaptedField

public class AdaptedField {

    private Field field;

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

}

演示版

导入java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Test.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Test test = (Test) unmarshaller.unmarshal(new File("input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(test, System.out);
    }

}

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<test>
   <fields>
      <field>
         <name>someValue</name>
         <value>true</value>
      </field>
   </fields>
   <fields>
      <field>
         <name>otherValue</name>
         <value>1</value>
      </field>
   </fields>
   <fields>
      <field>
         <name>anotherValue</name>
         <value>2</value>
      </field>
   </fields>
</test>
2020-12-02