users@jersey.java.net

Re: [Jersey] Re: Custom Invalid Parameter Handling

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 15 Apr 2010 15:04:31 +0200

Hi,

In addition to what Chris has said you can also do the following in
Jersey 1.1.5 or greater:

public class MyForm {
   ....

   public MyForm(
       @FormParam("email") final EmailAddress email,
       @FormParam("zipCode") final ZIPCode zipCode,
       @FormParam("phone") final TelephoneNumber phone) {
     ...
     // do 303 validation or other style
     // if fails throw an exception e.g. a WebApplicationException
with a
     // response or your own exception with an exception mapper
   }
}

@POST
@Path("create")
@Produces(MediaType.APPLICATION_JSON)
public String createAccount(@Inject MyForm f) { ... }

where @Inject is Jersey's @Inject (it would have been named
differently but it was created before JSR 330 was defined).


There is probably scope for a base class of such forms that provides
some generic validation support using 303 and reflection and the Java
beans API.


Another alternative is to use some AOP and validate parameters, but
IMHO that is not as independently testable.

Another alternative since this is a form is to create a message body
writer. But that is more low level and i think would require some
helper APIs to make this specific to form processing. In addition it
does not allow one to validate across multiple types of request
parameter, although that may be more of an edge case.

Hth,
Paul.

On Apr 15, 2010, at 1:20 AM, Chris Carrier wrote:

> Nope it's not handled transparently but you also don't call it for
> each parameter, you call it on the complete object after marshalling.
> Right now I'm only using this kind of validation for Post methods
> where Jersey is taking care of the marshalling but it'd work fine if
> you are marshalling manually. I'm using a generic superclass so I
> just have a 'create' method that all POSTs inherit and they pass the
> marshalled object up and it gets validated with the code I posted. If
> it's not annotated with validation annotations it'll just pass
> through.
>
> So far I've only dealt with domain classes that I created so I haven't
> run into the issue of not being able to annotate the classes. Though
> I really like how clean this approach is and it's gotten somewhat
> integrated into my platform so I would probably create a wrapper class
> or subclass or something like that to add the annotations and allow my
> validation code to do the rest. It's not great but it'd work. This
> approach probably works best where the majority of your classes you
> want to validate are accessible to you.
>
> Chris
>
> On Wed, Apr 14, 2010 at 4:03 PM, Dan Forward <dan-nabble_at_forwardhome.com
> > wrote:
>>
>> How do you hook this into Jersey? Do you have to call the validate
>> method for
>> each parameter at the top of each resource method or is it handled
>> transparently?
>>
>> What about classes you cannot annotate? For example, I use Joda
>> Time's
>> LocalDate for birth dates.
>>
>> Sincerely,
>>
>> Dan Forward
>> --
>> View this message in context: http://n2.nabble.com/Custom-Invalid-Parameter-Handling-tp4904335p4904617.html
>> Sent from the Jersey mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>