Dropwizard / Jersey: Authentication and JSON Option

I would like to have such a method for a resource REST interface

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

However, this is not possible because I get an error, for example: "SEVERE: There is no dependency for the public void method com.restaurant.FoodResource.add (com.restaurant.User, com.restaurant.Food) with the parameter with index 0 SEVERE: There is no dependency for method public void com.restaurant.FoodResource.add (com.restaurant.User, com.restaurant.Food) with parameter with index 1 SEVERE: method, public void com.restaurant.FoodResource.add (com.restaurant.User, com. restaurant.Food), an annotated POST resource, class com.restaurant.FoodResource, is not recognized as a valid resource method.

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

What are the options to do something like this? The ones that come to my mind are an object that encapsulates the user and food, but this does not seem like a pleasant solution for me.

Is there an even more elegant way to do something like this? Can I post a JSON Food view as @PathParam?

+6
source share
3 answers

I ran into the same problem. In fact, this was due to the lack of a provider in my test configuration.

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

For instance:

 addProvider(new BasicAuthProvider(<Authenticator>, <CacheBuilderSpec>)); 
+3
source

Section 3.3.2.1, despite this, I did not see errors, as you describe, by methods such as:

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

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

0
source

Try changing the parameters of your methods, from

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

to

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

Source: https://habr.com/ru/post/916296/


All Articles