Jaxb json转换从Jersey1.x转移到Jersey 2.x

时间:2016-07-08 07:18:25

标签: json jaxb jersey-2.0

请查看以下示例,该内容与Jersey 1.x和Jackson 1.x一样正常工作,但在迁移到Jersey 2.x和Jackson 2.x后,我在json输出中获得了xml。

 {
    "property": [{
        "attribs": ["<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<name>layoutType</name>", "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<display>Layout Type</display>", "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<type>COMBO</type>", "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<align>left</align>", "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<items>1 X 2,2 X 1,2 X 2</items>", "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<order>1</order>"]
    }, {
        "attribs": ["<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<name>chartTitle</name>", "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<display>Chart Title</display>", "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<type>TEXT</type>", "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<order>2</order>"]
    }]
}

代码: -

XML Input(attributes.xml):

<configuration>
                            <property>
                                <name>layoutType</name>
                                <display>Layout Type</display>
                                <type>COMBO</type>
                                <align>left</align>
                                <items>1 X 2,2 X 1,2 X 2</items>
                                <order>1</order>
                            </property>
                            <property>
                                <name>chartTitle</name>
                                <display>Chart Title</display>
                                <type>TEXT</type>
                                <order>2</order>`enter code here`
                            </property>
                        </configuration>

JAXB模型:

                @XmlRootElement(name = "configuration")
                public class ConfigModel {

                    private List<PropertyModel> properties;

                    public ConfigModel () {
                    }

                    @XmlElement(name = "property")
                    public List<PropertyModel> getProperties() {
                        return properties;
                    }

                    public void setProperties(List<PropertyModel> properties) {
                        this.properties = properties;
                    }
                }

            @XmlRootElement
            public class PropertyModel{
                private List<Object> attribs;

                public PropertyModel() {
                }

                @XmlAnyElement
                public List<Object> getAttribs() {
                    return attribs;
                }

                public void setAttribs(List<Object> attribs) {
                    this.attribs = attribs;
                }
            }

REST API:

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ConfigModel getAttributes() {
        ChartAttributeModel model = null;
        try {
            JAXBContext context = JAXBContext.newInstance(ConfigModel .class);
            model = (ConfigModel ) context.createUnmarshaller().unmarshal(new File("attributes.xml"));
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return model;
    }

预期产出:

     {
        "property": [{
            "name": "layoutType",
            "display": "Layout Type",
            "type": "COMBO",
            "align": "left",
            "items": "1 X 2,2 X 1,2 X 2",
            "order": "1"
        }, {
            "name": "chartTitle",
            "display": "Chart Title",
            "type": "TEXT",
            "order": "2"
        }]
     }

请帮忙。

谢谢

0 个答案:

没有答案