1

JAXBオブジェクトにマップする必要のある着信JSONがあります。JSONをXMLに変換してからJAXBオブジェクトにデータを入力する必要がありますか、それともJSONをオブジェクトに直接マップできますか?どちらの場合でも、誰かがこれを行うための最良のライブラリは何か教えてもらえますか?

4

4 に答える 4

0

Jacksonを使用している場合は、JacksonJAXBAnnotationsをここで確認できますhttp://wiki.fasterxml.com/JacksonJAXBAnnotations

于 2012-06-18T17:29:55.160 に答える
0

軽量なアプローチとして、StAXON - StAX 経由の JSON ( https://github.com/beckchr/staxon/ ) を使用できます。StAXON は JAXB のサポートを提供します。 https://github.com/beckchr/staxon/wiki/Using-JAXBを参照してください。

StAXON を使用すると、JAXB アノテーション付きモデルを JSON にバインドするのは次のように簡単です。

JsonXMLMapper<Person> mapper = new JsonXMLMapper<Person>(Person.class);
Person person = mapper.readObject(input);
...
mapper.writeObject(output, person);

StAXON は JAX-RS もサポートしています。 https://github.com/beckchr/staxon/wiki/Using-JAX-RSを参照してください。

于 2012-06-19T08:46:48.093 に答える
0

gsonは、JSON 文字列を既存の Java オブジェクトを含む同等の Java オブジェクトに変換できる柔軟なライブラリです。

于 2012-06-18T16:44:10.503 に答える
0

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

Do I need to convert the JSON to XML and then populate the JAXB object, or can I map the JSON directly to the object?

You do not need to convert the JSON to XML before you hand the input to a JAXB (JSR-222) implementation. If you are using the Jersey implementation (and probably others), you can set up your JAX-RS method like the following and JAXB will be used for both XML and JSON processing.

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("{id}")
public Customer read(@PathParam("id") long id) {
    return entityManager.find(Customer.class, id);
}

JAX-RS implementations have a MessageBodyReader/MessageBodyWriter mechanism that allow you to override the default conversion. I am not aware of Gson having any support for JAXB annotations. Jackson is a object-to-JSON library that offers support for a subset of JAXB annotations (maybe regrettably). EclipseLink MOXy is a JAXB implementation, that has been extended to support JSON-binding. For an example of using it in a JAX-RS environment see:

于 2012-06-18T18:25:43.217 に答える