MOXy元帅List<?>如何避免它在json结果中显示类型? [英] MOXy marshal List<?> how to avoid that it shows the type in json results?

查看:138
本文介绍了MOXy元帅List<?>如何避免它在json结果中显示类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我必须封送List<?>,如何避免它显示类型?

If I have to marshal a List<?> how to avoid that it shows the type?

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

So the result of a marshalling List<?> is [{"type" : "person","id":"1"},{"type" : "person","id":"2"}] } and it give me also the type="Person" in the JSON results!

如何避免显示类型?

谢谢

推荐答案

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

I haven't been able to reproduce the issue that you are seeing. Below is what I have tried.

域模型(人)

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的文件,并带有以下条目(请参阅:

To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: 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.

I don't have right now the code but I can see that the difference is that I define a metadata xml file where I say how to bind Person.

我仍然没有转载您的问题,但是这就是我如何修改我的示例.

I still haven't reproduced your issue, but here is how I have adapted my example.

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"
} ]

这篇关于MOXy元帅List&lt;?&gt;如何避免它在json结果中显示类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆