I am using a simple GET method to get customers (Jersey 1.7, using
Guice integration). Since to find a customer several things have to be
provided (id, country, city...), I decided to implement a simple
InjectableProvider to parse the query string into a Customer object:
@GET
@Path("customers")
public Result getResult(@ParseableFromQueryString final Customer
customer, @QueryParam format) {
// ...
}
This works perfect. Now, there is another requirement. We have
security restrictions on customer access. Not every user can see every
customer. I already have an AOP method interceptor bound with Guice.
This interceptor is based on annotations to decide where to validate
access. So, I changed my method to:
@GET
@Path("customers")
public Result getResult(@ParseableFromQueryString
@AssertCustomerAccess final Customer customer, @QueryParam format) {
// ...
}
And I get a 415: Invalid media type... After some debugging, I found
this piece of code in IntrospectionModeller:
/**
* Create a parameter from the list of annotations.
* Unknown annotated parameters are also supported, and in such a
* cases the last unrecognized annotation is taken to be that
* associated with the parameter.
*/
for (Annotation annotation : annotations) {
if (ANOT_HELPER_MAP.containsKey(annotation.annotationType())) {
// handle
} else if (Encoded.class == annotation.annotationType()) {
// handle
} else if (DefaultValue.class == annotation.annotationType()) {
// handle
} else {
paramAnnotation = annotation;
paramSource = Source.UNKNOWN;
paramName = getValue(annotation);
}
}
So, it seems that changing back the order of the annotations does the
trick, that is, changing the method to:
@GET
@Path("customers")
public Result getResult(@AssertCustomerAccess
@ParseableFromQueryString final Customer customer, @QueryParam format)
{
// ...
}
Works as expected. Now, I do not want to rely on annotation order for
my code to work... What can I do?
In other form questions I saw someone mentioning @InectableParam, but
still I am not sure how to tie my custom query parsing (it is not a
simple bean-setting operation, rather, a complex process of validating
parameters, translating properties, etc.)
--
Luis Javier de la Garza Treviņo