users@jersey.java.net

Re: [Jersey] several QueryParams ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 24 Sep 2009 10:57:34 +0200

On Sep 9, 2009, at 11:17 PM, Tatu Saloranta wrote:

> 2009/9/9 Felipe Gaścho <fgaucho_at_gmail.com>:
>> I finally adopted this:
>>
>> @POST
>> @Produces( { MediaType.APPLICATION_XML,
>> MediaType.APPLICATION_JSON })
>> public PujInstitutionEntity update(@QueryParam("acronym")
>> String acronym,
>> @QueryParam("name") String name,
>> @QueryParam("website") String website,
>> @QueryParam("logo") String logo,
>> @QueryParam("email") String email,
>> @QueryParam("contact") String contact,
>> @QueryParam("phone") String phone) {
>> PujInstitutionEntity institution = facade.read(
>> PujInstitutionEntity.class, acronym);
>> if (name != null) {
>> institution.setContact(validateNull(name));
>> }
> ...
>
> Not sure if this would be useful, but I found "Java Bean Validation"
> (JSR-303) -- and esp. RI from hibernate -- to be very useful for
> simple validation, for things like null checks, numeric range checks
> and such. Basically just let jersey (JAXB) bind data, have annotations
> for fields, use bean validator to see that everything got set as
> expected.
>

One thing you can do is create a class with fields annotated with the
@QueryParam.

And then obtain an instance of that class using ResourceContext:

https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/api/core/ResourceContext.html

        // Include bean validation annotations
        public static class MyParams {
            public MyParams(
            @QueryParam("acronym") String acronym,
            @QueryParam("name") String name,
            @QueryParam("website") String website,
            @QueryParam("logo") String logo,
            @QueryParam("email") String email,
            @QueryParam("contact") String contact,
            @QueryParam("phone") String phone ) {
                // Use bean validation here
             }
        }

        @POST
        @Produces( { MediaType.APPLICATION_XML,
MediaType.APPLICATION_JSON })
        public PujInstitutionEntity update(@Context ResourceContext
rc) {
           MyParams p = rc.getResource(MyParams.class);
        }

Paul.