NRaf
NRaf

Reputation: 7539

RESTful API - Serialization vs 'parsing'

I'm currently developing a RESTful API in Java using Jersey for an old, quite complex system. We are looking to support two forms of input - JSON and XML.

At this point I'm considering two ways of implementing the API - the first is to create a series of annotated POJOs to which the incoming request can be mapped to.

The second would be to convert any XML requests to JSON and parse the JSON manually.

Personally, the second way seems more flexible to me at this point, especially since some of the objects in the system are very complicated.

Basically I'm wondering if there are any benefits towards the first options (or drawbacks from the second) that I should consider?

EDIT: To elaborate a bit more, in my case, there are tons of classes which can't be annotated at this stage (this application has been actively developed for over ten years). If I'm going to go down the POJO route, I'm going to have to create a whole bunch of new 'serialization' objects whose purpose is to basically just to serialize to and from XML/JSON.

These classes will need to be managed to be kept up to do date with the actual model classes (of which there may be multiple representing the same object) which is why I'm thinking of going down the 'manual' method.

Were this a new project, I would definitely consider the use of annotations, but given the current situation, I'm unsure as to whether it is the best option.

Upvotes: 2

Views: 2534

Answers (2)

bdoughan
bdoughan

Reputation: 148977

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

I'm currently developing a RESTful API in Java using Jersey for an old, quite complex system. We are looking to support two forms of input - JSON and XML.

JAX-RS implementations make it easy to implement a service that takes both XML and JSON messages:

@PUT
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void put(Customer) {
    ...
}

By default Jersey uses a JAXB implementation with Jettison to convert objects to/from JSON:

MOXy supports native XML and JSON bindings using the JAXB runtime and annotations. This means you can map an object to XML and JSON using the same metadata:

To elaborate a bit more, in my case, there are tons of classes which can't be annotated at this stage (this application has been actively developed for over ten years).

If you have classes that cannot be modified to add annotations, then you can use MOXy's XML metadata document. Below is an example of how this metadata is leveraged in Jersey via a JAX-RS ContextResolver:

Upvotes: 2

Ryan Stewart
Ryan Stewart

Reputation: 128749

Both the benefit of the first and the drawback of the second is that there's very little work for you to do with the first method. POJOs with JAXB annotations are definitely the way to go with Jersey unless you just can't make it work--like if you just can't get it to represent the XML/JSON that you want it to. In general, though, you get XML and JSON input and output virtually for free.

Upvotes: 3

Related Questions