New in JAX-RS 2.0 – @BeanParam annotation

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. 11

     

    1_1

     

  3. 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

 

21

 

33

 

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

41

 

Make sure that we include the @BeanParam annotation while injecting a reference variable of this POJO (only on METHOD, PARAMETER or FIELD).

5

 
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!

 
6

 

7

 

That’s it. . . Short and Sweet!

Keep Coding!

Reference: New in JAX-RS 2.0 – @BeanParam annotation from our JCG partner Abhishek Gupta at the Object Oriented.. blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "New in JAX-RS 2.0 – @BeanParam annotation"

  1. Riccardo says:

    That’s cool, I’ve missed something like this in JAX-RS 1, so much!

Leave a Reply


seven + 9 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close