JAX-RS is awesome to say the least and one of my favorites! Why?
- Feature rich
- Intuitive (hence the learning curve is not as steep)
- Easy-to-use and develop with
- Has great RIs – Jersey, RestEasy etc
There are enough JAX-RS fans out there who can add to this! đ
JAX-RS 2.0 is the latest version of the JSR 311 specification and it was released along with Java EE 7.
Life without @BeanParam
Before JAX-RS 2.0, in order to pass/inject information from an HTTP request into JAX-RS resource implementation methods, one could
1. Include multiple method arguments annotated with @FormParam, @PathParam, @QueryParam etc
2. Or, have a model class backed by JAXB/JSON or a custom MessageBodyReader implementation for JAX-RS Provider to be able to unmarshall the HTTP message body to a Java object – read more about this in one of my previous posts
This means that something like a HTML5 based client would need to extract the FORM input, convert it into JSON or XML payload and then POST it over the wire.
Simplification in JAX-RS 2.0
This process has been simplified by introduction of the @BeanParam annotation. It helps inject custom value/domain/model objects into fields or method parameters of JAX-RS resource classes.
In case you want to refer to the code (pretty simple) or download the example/run it yourself, here is the GitHub link
All we need to do is, annotate the fields of the model (POJO) class with the injection annotations that already exist i.e. @PathParam, @QueryParam, @HeaderParam, @MatrixParam etc – basically any of the @xxxParam metadata types and
Make sure that we include the @BeanParam annotation while injecting a reference variable of this POJO (only on METHOD, PARAMETER or FIELD).
JAX-RS provider automatically constructs and injects an instance of your domain object which you can now use within your methods.
Just fill in the form information and POST it !
That’s it. . . Short and Sweet đ
Keep Coding !
Buddy, you just copy and pasted this tutorial from this link : http://java.dzone.com/articles/new-jax-rs-20-%E2%80%93-beanparam
Please give some credit to original author.
Peace.
LikeLike
Hi Avinash – it just happens to be a coincidence that the author of this post and the one you pointed ( http://java.dzone.com/articles/new-jax-rs-20-%E2%80%93-beanparam) are the same.
Giving credit to self for a republished blog of mine on a different site ?? Not my style đ
Best Regards always! Thanks for visiting…
LikeLike
Can you please explain the advantage of using BeanParam when compared to using JAXB annotated POJO ? When BeanParam is used, there is no marshalling/unmarshalling happening in background?
LikeLike
@BeanParam and JAXB support (in JAX-RS) are two different topics altogether. The former makes it easy to inject data from various sources (headers, cookies etc.) by allowing you to represent them as a a single POJO (bean), rather than injecting them individually. To answer your question, there is no marshalling/unmarhshalling happening here. JAX-RS runtime just injects the POJO. Since JAXB support is provided by JAX-RS implementations by default, the serialization/deserialzation is taken care of by the runtime/framework itself.
Hope this helped
Cheers!
LikeLike
How we pass this it at client side i mean URL
LikeLike
Pallavi
It depends on what you’re trying to inject i.e. headers, cookies, query parameters etc. All these need to passed from the client in the standard ‘HTTP’ style.
LikeLike
Hi Abhishek,
Can BeanParam support JSON payload? If not then is there any other annotation which can wrap all the xxxParam and post request’s JSON payload in a single object?
LikeLike
JSON payload support is available in JAX-RS 2.0 using the JSON-P API
LikeLike