users@jersey.java.net

[Jersey] jersey-binding module

From: George Sapountzis <gsapountzis_at_gmail.com>
Date: Fri, 5 Aug 2011 21:01:09 +0300

Hi,

# Module

I stared a utility module for using JAX-RS resources as MVC
controllers. The module adds two things:

* BindingError: in case of a binding error for a query or form param,
do not throw an exception but add an error to a per-request
Set<BindingError> and continue binding.

* ParamBean: support (non-annotated) "bean" types in addition to the
"string" types defined in the spec for @QueryParam and @FormParam
binding.

The source code is available at
[jersey-binding](https://github.com/gsapountzis/jersey-binding).

# Implementation

The implementation consists of the following packages:

* error

  @FormParam decorator for BindingError functionality.

* bean

  @FormParam decorator ParamBean functionality.

* model

  Copy of com.sun.jersey.api.model, keep only code related to fields
and setter methods.

* component

  Copy of com.sun.jersey.server.spi.component, keep only code related
to fields and setter methods.

* util

  Utilities for detecting if a type is a "string" type as defined in the spec.

  Utilities for creating annotation literals (also copied, from CDI this time).

So, there was a little bit of programming by duplicating and partially
deleting code in the model and component packages :-)

I am not happy with the copied code, but I initially looked at
implementing ParamBeans as a special case of per-request abstract
resources but quickly got lost in all the resource and component
related stuff, so I punted. Also, it feels that AbstractResource
should be extending a basic Bean/Component abstraction (similar to
ParamBean), not the other way around. So I guess I'll be watching with
interest the space of JAX-RS 2.0 integration with JSR-330 and its
implementation within Jersey. Lastly, nested ParamBeans are actually
dependent on their parent beans not request-scoped which is the case
for abstract resources, which contributed to my confusion as well.

# TODO

There are a few missing things, most are noted in the code also. Off
the top of my head, the most important are:

* configuration (resource method, binder)
* support for List and Map
* query beans
* message body reader for form beans
* compose the BindingError and ParamBean decorators
* ...

# Jersey patches

I added a few patches to Jersey along the way, available at
[Jersey](https://github.com/gsapountzis/jersey).

The most important from the jersey-binding point of view is
[](https://github.com/gsapountzis/jersey/commit/aacf38a601ebb128559a2102efdecc6336521cc5)
which add an extractDefaultValue() method to
MultivaluedParameterExtractor.

Then, there is [](https://github.com/gsapountzis/jersey/commit/d1d86ec18a15f3ae778ca9636e3fa0271e5908f3)
which adds (non-standard) support for using Jersey+CDI on tomcat.

Lastly, there are a couple of dispatch related patches. These changes
are mostly naming cleanups and moving classes around. There is one
important change there
[](https://github.com/gsapountzis/jersey/commit/1776d4bca6bcce4551842abd74afa930e8d89595),
namely refactoring EJBMethodDispatcher as an Adapter. The dispatcher
changes are the kind of changes that make seem good to a newcomer's
eye but probably look like unnecessary churn to project developers,
please take a look and evaluate ...

All in all, thanks for a great project and a great spec.

Regards,
George.