问题描述
要解决另一个问题,我已经从使用转到从JAXB创建的对象模型生成JSON(由Sun JAXB 2.1.12创建)。我注意到的一个区别是,在对象模型中,数字属性被定义为
To solve another problem I have moved from using Jersey to EclipseLink MOXy to generate JSON from a JAXB created object model ( created by Sun JAXB 2.1.12). One difference I've noticed is that in the object model numeric attribute are defined as
@XmlSchemaType(name = "nonNegativeInteger")
protected BigInteger count;
泽西转换为
"count":1,
但MOXy给出
"count" : "1",
如何让MOXy实现其数字字段而不是引用它。
How can I get MOXy to realize its a numeric field and not quote it.
推荐答案
更新
已修复EclipseLink 2.4.1和2.5.0流中的修复程序。您可以从以下链接下载包含此修复程序的夜间标签 2012年7月13日:
A fix has been checked into the EclipseLink 2.4.1 and 2.5.0 streams. You can download a nightly label containing this fix starting July 13, 2012 from the following link:
- http://www.eclipse.org/eclipselink/downloads/nightly.php
将数字类型封送到JSON而不带引号。在这种情况下, @XmlSchemaType
注释的存在会导致问题。这是一个错误,你可以使用以下链接来跟踪我们在这个问题上的进展:
EclipseLink JAXB (MOXy) will marshal numeric types to JSON without quotes. In this case the presence of @XmlSchemaType
annotation is causing a problem. This is a bug and you can use the following link to track our progress on this issue:
- http://bugs.eclipse.org/384919
替代方法
MOXy的外部映射文档可用于覆盖字段/属性级别的映射。我们将利用它来重新映射 count
属性,以删除有问题的 @XmlSchemaType
注释。
MOXy's external mapping document can be used to override mappings at the field/property level. We will leverage this to remap the count
property to remove the problematic @XmlSchemaType
annotation.
oxm.xml
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11448966">
<java-types>
<java-type name="Root">
<java-attributes>
<xml-element java-attribute="count"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Root
package forum11448966;
import java.math.BigInteger;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlSchemaType(name = "nonNegativeInteger")
protected BigInteger count;
}
jaxb.properties
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
package forum11448966;
import java.math.BigInteger;
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.OXM_METADATA_SOURCE, "forum11448966/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);
Root root = new Root();
root.count = BigInteger.TEN;
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
输出
{
"count" : 10
}
这篇关于如何通过MOXy生成Json来了解模型何时是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!