Java MongoDB、编组、JAXB和JSON

Java MongoDB、编组、JAXB和JSON,java,mongodb,jaxb,jersey,marshalling,Java,Mongodb,Jaxb,Jersey,Marshalling,所以我真的很困惑,所以希望我所问的有意义。我应该注意到,这是家庭作业,但我只是要求帮助的一小部分 以下是我的数据类的简化和通用版本: @XmlRootElement(name="item") @XmlType(propOrder={"name", "value"}) public class Item { private String name; private int value; @XmlElement(name="name") public St

所以我真的很困惑,所以希望我所问的有意义。我应该注意到,这是家庭作业,但我只是要求帮助的一小部分

以下是我的数据类的简化和通用版本:

@XmlRootElement(name="item")
@XmlType(propOrder={"name", "value"})
public class Item {

    private String name;
    private int value;

    @XmlElement(name="name")    
    public String getName() {
        return name;
    }

    @XmlElement(name="value")   
    public int getValue() {
        return value;
    }

}

@XmlRootElement(name=“itemcontainer”)
@XmlType(proporter={“name”,“items”})
公共类ItemContainer{
私有字符串名称;
私有ArrayList项;
@xmlement(name=“name”)
公共字符串getName(){
返回名称;
}
@xmlement(name=“items”)
公共阵列列表getItem(){
退货项目;
}
}
我需要能够在JSON之间来回转换ItemContainer对象,以存储在MongoDB数据库中。(必须是MongoDB)

据我所知,数组列表无法编组

我最终需要做的是从MongoDB数据库中读取,将JSON解组为Java对象,对这些对象执行操作,然后将它们重新打包为JSON存储在数据库中


要在JSON和ItemContainer之间进行转换,我需要做哪些更改或操作?

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

下面是如何使用MOXy的JSON绑定来支持您的用例

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/forum15728638/input.json");
        List<Foo> foos = (List<Foo>) unmarshaller.unmarshal(json, Foo.class).getValue();

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

}
下面是我们将用于此示例的Java模型

public class Foo {

    private String bar;

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

}
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/forum15728638/input.json");
        List<Foo> foos = (List<Foo>) unmarshaller.unmarshal(json, Foo.class).getValue();

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

}
了解更多信息


如何将JSON插入数据库?我看不到任何函数会接受要插入的字符串(表示JSON)。在我的应用程序中,我必须在
package info.java
中指定名称空间信息,只有
@XmlPath
字段映射到DTO。是否需要指定?这有什么用?
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/forum15728638/input.json");
        List<Foo> foos = (List<Foo>) unmarshaller.unmarshal(json, Foo.class).getValue();

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

}
[ {
   "bar" : "Hello"
}, {
   "bar" : "World"
} ]