Json 带有一个元素的jaxb列表

Json 带有一个元素的jaxb列表,json,list,jaxb,singleton,Json,List,Jaxb,Singleton,我生成JSON,其中包含一个列表成员。它被整理好了 然而,当列表只有一个元素时,我的消费方(第三方)抱怨缺少[]-对。我生产的产品是: "mylist":{"id":104,"name":"Only one found"} // produced 而我的消费者期望: "mylist":[{"id":104,"name":"Only one found"}] // expected by third party 我的实现是否产生了错误的JSON?注意:我是专家组的负责人和成员 JAXB(JSR

我生成JSON,其中包含一个列表成员。它被整理好了

然而,当列表只有一个元素时,我的消费方(第三方)抱怨缺少[]-对。我生产的产品是:

"mylist":{"id":104,"name":"Only one found"} // produced
而我的消费者期望:

"mylist":[{"id":104,"name":"Only one found"}] // expected by third party

我的实现是否产生了错误的JSON?

注意:我是专家组的负责人和成员

JAXB(JSR-222)规范不包括JSON绑定。您看到的这种行为很可能是由于JAXB实现与像Jettison这样的库一起使用。抛弃将StAX事件转换为JSON或从JSON转换为StAX事件,并且只能在元素多次出现时检测列表(请参见:)。EclipseLink JAXB提供本机JSON绑定,可以正确表示大小为1的数组

JAVA模型

Foo

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

}
import java.util.List;
导入javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
公开课Foo{
私人名单;
}

import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
公共类酒吧{
私有int-id;
私有字符串名称;
}
jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中包含一个名为
JAXB.properties
的文件,其中包含以下条目(请参见:):

演示代码

演示

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

}
其他信息


感谢您详尽的回答。谢谢。既然Blaise暗示了具体的实现,我在TomEE论坛上问道。似乎在我现在使用的TomEE+1.5.1之前存在问题:。我的问题还没有解决,但现在我有了解决它的线索。
{
   "mylist" : [ {
      "id" : 104,
      "name" : "Only one found"
   } ]
}