アップデート
修正がEclipseLink2.4.1および2.5.0ストリームにチェックインされました。次のリンクから、2012年7月13日以降、この修正を含む夜間ラベルをダウンロードできます。
EclipseLink JAXB(MOXy)は、数値型を引用符なしでJSONにマーシャリングします。この場合、@XmlSchemaType
注釈の存在が問題を引き起こしています。これはバグであり、次のリンクを使用して、この問題の進捗状況を追跡できます。
回避策
MOXyの外部マッピングドキュメントを使用して、フィールド/プロパティレベルでマッピングを上書きできます。これを利用してプロパティを再マップし、問題のあるアノテーションcount
を削除します。@XmlSchemaType
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>
根
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
}