New in JAX-RS 2.0 – @BeanParam Annotation
JAX-RS 2.0 is the latest version of the JSR 311 specification and it was released along with Java EE 7.
Join the DZone community and get the full member experience.
Join For Freejax-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 :-)
Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments