これを使用して、JAXB Bean を JSON コードに変換します。
private String marshall(final Book beanObject) throws Exception
{
JAXBContext context = JAXBContext.newInstance(Book.class);
Marshaller marshaller = context.createMarshaller();
Configuration config = new Configuration();
MappedNamespaceConvention con = new MappedNamespaceConvention(config);
StringWriter jsonDocument = new StringWriter();
XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, jsonDocument);
marshaller.marshal(beanObject, xmlStreamWriter);
return jsonDocument.toString();
}
私の Book クラスの場合、出力は次のようになります。
{"bookType":{"chapters":["Genesis","Exodus"],"name":"The Bible","pages":600}}
ただし、出力をJerseyと互換性があるようにしたい:
{"chapters":["Genesis","Exodus"],"name":"The Bible","pages":600}
上記のコードで 2 番目の JSON 表記をアーカイブし、ルート要素を取り除くにはどうすればよいですか?
私の解決策:
Jackson に切り替えて、ルート アンラップのオプションを設定できます。ただし、Jettison ソリューションがあれば、まだ興味があります。