通过 Jettison 将 Java 对象(不带 @XmlRootElement)编组到 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15402659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:27:40  来源:igfitidea点击:

Marshalling the Java objects (without @XmlRootElement) to JSON via Jettison

javajsonjakarta-eejaxbjettison

提问by Arun

I have done the marshalling of an JAXB object (Which contains @XmlRootElement) to JSON using Jettison. But I can not convert a simple java object which has no annotations like @XmlRootElement to JSON. I would like to know "Is it mandatory to have that @XmlRootElement to marshall an object to JSON?"

我已经使用 Jettison 将 JAXB 对象(其中包含 @XmlRootElement)编组到 JSON。但是我无法将没有像@XmlRootElement 这样的注释的简单 java 对象转换为 JSON。我想知道“是否必须让 @XmlRootElement 将对象编组为 JSON?”

I am getting the following Exception when I try to marshall the java object to Json

当我尝试将 java 对象编组到 Json 时,出现以下异常

com.sun.istack.SAXException2: unable to marshal type "simpleDetail" as an element because it is missing an @XmlRootElement annotation

What could be the issue?

可能是什么问题?

采纳答案by bdoughan

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

注意:我是EclipseLink JAXB (MOXy) 的负责人,也是JAXB (JSR-222)专家组的成员。

The JAXB (JSR-222) specification does not cover JSON-binding. Instead of using a JAXB implementation with the Jettison library, you could use EclipseLink JAXB (MOXy) that offers native JSON-binding. Below is an example.

JAXB (JSR-222) 规范不包括 JSON 绑定。您可以使用提供原生 JSON 绑定的 EclipseLink JAXB (MOXy),而不是将 JAXB 实现与 Jettison 库一起使用。下面是一个例子。

JAVA MODEL

爪哇模型

Foo

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    private List<Bar> mylist;

}

Bar

酒吧

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {

    private int id;
    private String name;

}

jaxb.properties

jaxb.properties

To specify MOXy as your JAXB provider you need to include a file called jaxb.propertiesin the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

要将 MOXy 指定为您的 JAXB 提供程序,您需要包含一个jaxb.properties在与域模型相同的包中调用的文件,其中包含以下条目(请参阅:http: //blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as -your.html):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

DEMO CODE

演示代码

Demo

演示

MOXy does not require the @XmlRootElementannotation, and you can use the JSON_INCLUDE_ROOTproperty to tell MOXy to ignore the presence of any @XmlRootElementannotations. When the root element is ignored you need to use an unmarshalmethod that takes a class parameter to specify the type you are unmarshalling.

MOXy 不需要@XmlRootElement注释,您可以使用该JSON_INCLUDE_ROOT属性告诉 MOXy 忽略任何@XmlRootElement注释的存在。当根元素被忽略时,您需要使用一个unmarshal带有类参数的方法来指定您要解组的类型。

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}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource("src/forum15404528/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/Output

输入.json/输出

We see that no root element is present in the input or output.

我们看到输入或输出中不存在根元素。

{
   "mylist" : [ {
      "id" : 104,
      "name" : "Only one found"
   } ]
}

ADDITIONAL INFORMATION

附加信息

回答by Jeevan Patil

Just read that @XmlRootElementis not necessary always. Please read this blog, at the bottom you will find how it's done without @XmlRootElement. Also go through the answers in the post No @XmlRootElement generated by JAXB.

只是阅读这@XmlRootElement并不总是必要的。请阅读此博客,在底部您会发现没有@XmlRootElement. 还请查看由 JAXB 生成的 No @XmlRootElement帖子中的答案。