1

別の問題を解決するために、JerseyからEclipseLink MOXyに移行して、JAXBで作成されたオブジェクトモデル(Sun JAXB 2.1.12で作成)からJSONを生成しました。私が気付いた違いの1つは、オブジェクトモデルでは数値属性が次のように定義されていることです。

@XmlSchemaType(name = "nonNegativeInteger")
protected BigInteger count;

ジャージーはに変換します

"count":1,

しかし、MOXyは

"count" : "1",

MOXyに数値フィールドを認識させ、引用符で囲まないようにするにはどうすればよいですか。

4

1 に答える 1

1

アップデート

修正が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
}
于 2012-07-12T11:00:41.007 に答える