Java 使用MoXY在XML和JSON中对列表进行sane表示
我需要在XML和JSON中公开JAX-RS资源,其中一部分将包括传入(可能较大的)整数列表。我使用MoXY作为我的JAXB/JSON提供程序 我遇到的问题是,我不知道如何公开在XML和JSON中都能正常工作的整数列表 如果我使用Java 使用MoXY在XML和JSON中对列表进行sane表示,java,xml,json,jaxb,moxy,Java,Xml,Json,Jaxb,Moxy,我需要在XML和JSON中公开JAX-RS资源,其中一部分将包括传入(可能较大的)整数列表。我使用MoXY作为我的JAXB/JSON提供程序 我遇到的问题是,我不知道如何公开在XML和JSON中都能正常工作的整数列表 如果我使用 @XmlList @XmlElement(name = "Values", type = Integer.class) protected List<Integer> values; 这在处理大量数字时是不可取的。如果省略@XmlList注释,则正确处理J
@XmlList
@XmlElement(name = "Values", type = Integer.class)
protected List<Integer> values;
这在处理大量数字时是不可取的。如果省略@XmlList注释,则正确处理JSON,但XML
...
<Values>0 1 2 4</Values>
...
但这就带来了非常繁琐的XML格式
...
<Values>
<Value>0</Value>
<Value>1</Value>
<Value>2</Value>
<Value>4</Value>
</Values>
...
。。。
0
1.
2.
4.
...
有人对如何进行有什么建议吗?我无法想象这是一个不常见的用例,但我完全被难住了。下面是一个如何处理用例的示例: Java模型(Foo) 这是一个示例类,用于保存问题中描述的属性
package forum23939109;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlList
@XmlElement(name = "Values", type = Integer.class)
protected List<Integer> values;
public List<Integer> getValues() {
return values;
}
public void setValues(List<Integer> values) {
this.values = values;
}
}
输出
下面是运行演示代码的输出:
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<Values>0 1 2 4</Values>
</foo>
{
"Values" : [ 0, 1, 2, 4 ]
}
0 1 2 4
{
“值”:[0,1,2,4]
}
这就行了,谢谢!但是为什么?我觉得我没有告诉MoXY这里有什么新东西。@mookpoint-所有外部映射文件都告诉MoXY从属性中删除@XmlList
。
...
<Values>
<Value>0</Value>
<Value>1</Value>
<Value>2</Value>
<Value>4</Value>
</Values>
...
package forum23939109;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlList
@XmlElement(name = "Values", type = Integer.class)
protected List<Integer> values;
public List<Integer> getValues() {
return values;
}
public void setValues(List<Integer> values) {
this.values = values;
}
}
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum23939109">
<java-types>
<java-type name="Foo">
<java-attributes>
<xml-element java-attribute="values" name="Values"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
package forum23939109;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
// Create Instance of Domain Model
Foo foo = new Foo();
ArrayList<Integer> values = new ArrayList<Integer>();
values.add(0);
values.add(1);
values.add(2);
values.add(4);
foo.setValues(values);
// Marshal Object to XML Based on Annotations
JAXBContext jcXML = JAXBContext.newInstance(Foo.class);
Marshaller marshallerXML = jcXML.createMarshaller();
marshallerXML.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshallerXML.marshal(foo, System.out);
// Marshal Object to JSON Based on Annotations & External Mappings
Map<String, Object> properties = new HashMap<String, Object>(3);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum23939109/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, org.eclipse.persistence.oxm.MediaType.APPLICATION_JSON);
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jcJSON = JAXBContext.newInstance(new Class[] {Foo.class}, properties);
Marshaller marshallerJSON = jcJSON.createMarshaller();
marshallerJSON.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshallerJSON.marshal(foo, System.out);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<Values>0 1 2 4</Values>
</foo>
{
"Values" : [ 0, 1, 2, 4 ]
}