MOXy marshal List <! - ? - >如何避免它显示json结果中的类型?

时间:2013-06-06 16:02:30

标签: json eclipselink moxy

如果我必须整理一个List<?>如何避免它显示类型?

所以编组List<?>的结果是[{"type" : "person","id":"1"},{"type" : "person","id":"2"}] },它在JSON结果中也给了我type =“Person”!

我怎么能避免它向我显示类型?

谢谢

1 个答案:

答案 0 :(得分:0)

我无法重现您所看到的问题。以下是我的尝试。

域名模型(人)

package forum16966861;

public class Person {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

<强> jaxb.properties

要将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

<强>演示

package forum16966861;

import java.util.*;
import javax.xml.bind.*;
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[] {Person.class}, properties);

        List<Object> people = new ArrayList<Object>(2);

        Person jane = new Person();
        jane.setId(1);
        jane.setName("Jane");
        people.add(jane);

        Person john = new Person();
        john.setId(2);
        john.setName("John");
        people.add(john);

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

}

<强>输出

[ {
   "id" : 1,
   "name" : "Jane"
}, {
   "id" : 2,
   "name" : "John"
} ]

<强>更新

  

我现在没有代码,但我可以看到差异   我定义了一个元数据xml文件,其中我说如何绑定Person。

我仍然没有重现你的问题,但这是我如何调整我的例子。

<强> oxm.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum16966861">
    <java-types>
        <java-type name="Person">
            <xml-root-element/>
        </java-type>
    </java-types>
</xml-bindings>

<强>演示

package forum16966861;

import java.util.*;
import javax.xml.bind.*;
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>(3);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum16966861/oxm.xml");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance("forum16966861", Person.class.getClassLoader(), properties);

        List<Object> people = new ArrayList<Object>(2);

        Person jane = new Person();
        jane.setId(1);
        jane.setName("Jane");
        people.add(jane);

        Person john = new Person();
        john.setId(2);
        john.setName("John");
        people.add(john);

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

}

<强>输出

[ {
   "id" : 1,
   "name" : "Jane"
}, {
   "id" : 2,
   "name" : "John"
} ]