Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard/Jersey: Authentication and JSON parameter

I'd like to have a method like this for the REST interface of a resource

@POST
@Consumes(MediaType.APPLICATION_JSON)
public void add(@Auth User user, @Valid Food food) {
    //consume
}

However, this is not possible as I get an error like: "SEVERE: Missing dependency for method public void com.restaurant.FoodResource.add(com.restaurant.User, com.restaurant.Food) at parameter at index 0 SEVERE: Missing dependency for method public void com.restaurant.FoodResource.add(com.restaurant.User, com.restaurant.Food) at parameter at index 1 SEVERE: Method, public void com.restaurant.FoodResource.add(com.restaurant.User, com.restaurant.Food), annotated with POST of resource, class com.restaurant.FoodResource, is not recognized as valid resource method.

I found that it is not possible to have two parameters not annotated with certain annotations according to the specifications (3.3.2.1 Entity Parameters) http://jsr311.java.net/nonav/releases/1.1/spec/spec3.html

What options exist to still do something like this? The ones that come to my mind is an object that encapsulates User and Food, but that doesn't seem to be like a nice solution to me.

Is there any other more elegant way to do something like this? Can i send the JSON Food representation as @PathParam?

like image 571
Hansi Avatar asked May 21 '12 19:05

Hansi


1 Answers

I faced the same problem. In fact this was due to a missing provider in my test configuration.

In your resource unit test in the setUpResources() method, use the method addProvider() to set your authentication provider.

For example:

addProvider(new BasicAuthProvider(<Authenticator>, <CacheBuilderSpec>));
like image 131
jdcoffre Avatar answered Nov 15 '22 10:11

jdcoffre