users@jersey.java.net

[Jersey] Parsing/conversion errors reporting (with MVC)

From: George Sapountzis <gsapountzis_at_gmail.com>
Date: Thu, 7 Jul 2011 17:13:10 +0300

Hi,

I am playing with using JAX-RS as an MVC framework. Currently,
parsing/conversion errors are reported by throwing an exception. While
this is convenient for REST services, it is very inconvenient for MVC
controllers,
consider the basic use case of a form submission with errors which
needs to be redisplayed.

How do people handle the above case in the current frameworks ? Is
there an example ?

A few thoughts on the issue:

The problem affects both resource method parameters (e.g. query and
form params) and message bodies (e.g. form-urlencoded or json).

A possible approach to solve the issue is to provide a custom
injectable (@Context) object that collects the parsing errors. This
object is populated by the parameter injectors and message body
readers and is consumed by the resource method. This seems possible
but requires a custom injectable object and decorating the stock
parameter injectors and message body readers, which sounds more work
than it should for such a basic use case.

Wrt validation, the JSR 2.0 spec page states: "This JSR will make it
possible to validate a set of parameters, such as form or query
parameters, and to return a meaningful response in case validation
fails". I think the above argument about conversion errors holds here,
as well. From an MVC pov, it is much more convenient to make the
violations available to the resource method as a context object rather
than call a response mapper for validation exceptions.

thanks,
George