JAXB映射到JSON [英] JAXB Mapping to JSON

查看:171
本文介绍了JAXB映射到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个JAX-RS(Jersey)REST服务,它接受ONIX XML格式的XML消息。通常,我已经使用xjc从给定模式生成了JAXB绑定的所有必需类。整体上有500多个课程,我无法修改它们。

I have written a JAX-RS (Jersey) REST Service, which accepts XML messages of ONIX XML format. Generally, I have generated all the required classes for JAXB binding from the given schema with xjc. There are more than 500 classes overall and I cannot modify them.

现在,当我有一个JAXB映射对象时,我需要将它存储到数据库中。我使用mongoDb,因此消息格式应该是JSON。我尝试使用Jackson和JAXB模块将JAXB对象转换为JSON,这对于存储数据非常有用。但是当我尝试将JSON转换回JAXB对象时,它会以某种方式抛出与JAXBElement连接的异常。在谷歌我发现杰克逊不支持JAXBElement,我必须解决这个问题。但是我无法做到这一点,因为我无法修改JAXB生成的类。

Now, when I have a JAXB-mapped object, I need to store it to the database. I work with mongoDb, so the message format should be JSON. I tried to use Jackson with JAXB module to convert JAXB-object into JSON, which works pretty fine with storing the data. But when I try to convert the JSON back into the JAXB object, it throws an exception connected somehow with the JAXBElement. In google I found out that the JAXBElement is not supported in Jackson and I have to work around this issue. But I cant do it because I cannot modify JAXB-generated classes.

有没有办法用其他方法将JAXB对象映射到JSON,但是它会跟随整个JAXB规范,以便将来从JSON转换为JAXB对象和签证没有问题?

Is there a way to map JAXB Objects into JSON with some other means, but which will follow the whole JAXB specification so that I have no problems in the future converting from JSON to the JAXB object and visa vera?

推荐答案

注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

如果你不能与Jackson合作,你的用例将适用于MOXy。

If you can't do it with Jackson, you use case will work with MOXy.

Foo

这是一个示例类,其中包含 JAXBElement 类型的字段。

Here is a sample class that contains a field of type JAXBElement.

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    @XmlElementRef(name="bar")
    private JAXBElement<Bar> bar;

}

酒吧

public class Bar {

}

ObjectFactory

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name = "bar")
    public JAXBElement<Bar> createBar(Bar bar) {
        return new JAXBElement<Bar>(new QName("bar"), Bar.class, bar);
    }

}



独立演示代码



下面是一些可以在Java SE中运行的演示代码,看看一切正常:

Standalone Demo Code

Below is some demo code you can run in Java SE to see that everything works:

Demo

import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class, ObjectFactory.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource("src/forum19158056/input.json");
        Foo foo = unmarshaller.unmarshal(json, Foo.class).getValue();

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

}

input.json /输出

{"bar" : {} }



使用JAX-RS运行



以下链接将帮助您在JAX-RS服务中利用MOXy :

Running with JAX-RS

The following links will help you leverage MOXy in a JAX-RS service:

  • http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html
  • http://blog.bdoughan.com/2013/06/moxy-is-new-default-json-binding.html

这篇关于JAXB映射到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆