Hansi
Hansi

Reputation: 101

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?

Upvotes: 6

Views: 2596

Answers (3)

AlexandrosD
AlexandrosD

Reputation: 555

Try to swap your methods parameters, from

public void add(@Auth User user, @Valid Food food)

to

public void add(@Valid Food food, @Auth User user)

Upvotes: 0

jdcoffre
jdcoffre

Reputation: 41

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>));

Upvotes: 3

user486734
user486734

Reputation:

Section 3.3.2.1 notwithstanding, I haven't seen errors like you describe with methods such as the following:

public Response post(
    @Auth User user, @Valid Food food, @Context UriInfo uriInfo);

public Response post(
    @Auth User user, @Valid Food food, @QueryParam String s);

public Response post(
    @Auth User user, @Valid Food food, BooleanParam b);

So a workaround might be to add an unused @QueryParam or @Context parameter.

Upvotes: 0

Related Questions