我需要以 XML 和 JSON 形式公开 JAX-RS 资源,其中一部分将包括传入(可能很大)整数列表。我使用 MoXY 作为我的 JAXB/JSON 提供程序。
我遇到的问题是我无法弄清楚如何公开在 XML 和 JSON 中都能很好地工作的整数列表。
如果我使用
@XmlList
@XmlElement(name = "Values", type = Integer.class)
protected List<Integer> values;
然后 JSON 被(未)编码为
...
"Values" : "0 1 2 4"
...
在处理大量数字时这是不可取的。如果省略@XmlList注释,则可以正确处理JSON,但可以正确处理XML
...
<Values>0 1 2 4</Values>
...
被解析为 [124] 而不是 [0, 1, 2, 4]。
通过使用@XmlElementWrapper(并告诉MoXY“useWrapperAsArrayName”)
@XmlElementWrapper(name = "Values", required = true)
@XmlElement(name = "Value")
protected List<Object> values = new ArrayList<Object>();
实现了良好的 JSON
...
"Values" : [0, 1, 2, 4]
...
但这会强加非常繁琐的 XML 格式
...
<Values>
<Value>0</Value>
<Value>1</Value>
<Value>2</Value>
<Value>4</Value>
</Values>
...
有人对如何进行有任何建议吗?我无法想象这是一个不常见的用例,但我完全被难住了。
最佳答案
下面是如何处理您的用例的示例:
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;
}
}
外部元数据 (oxm.xml)
<?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>
演示代码
演示
下面的演示代码演示了如何在创建用于处理 JSON 内容的 JAXBContext
时利用外部映射文档。
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 ]
}
关于java - 使用 MoXY 以 XML 和 JSON 形式对列表进行合理表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23939109/