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

查看:26
本文介绍了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.我尝试使用带有 JAXB 模块的 Jackson 将 JAXB 对象转换为 JSON,这在存储数据方面效果很好.但是当我尝试将 JSON 转换回 JAXB 对象时,它会抛出一个与 JAXBElement 以某种方式连接的异常.在 google 中,我发现 Jackson 不支持 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 {

}

对象工厂

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:

演示

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:

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

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