1

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

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

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

谢谢

4

1 回答 1

1

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

领域模型(人)

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"
} ]
于 2013-06-06T18:09:24.643 回答