如何使用jax-rs将bean结构映射到不同的模式

时间:2011-05-04 09:06:38

标签: java jaxb jax-rs jaxb2

我有这个豆

@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注释?

我正在使用运动衫,但答案并不一定是具体的。

1 个答案:

答案 0 :(得分: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;
    }

}

<强> FieldsAdapter

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

}

<强>演示

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

了解更多信息