Dropwizard / Jersey:身份验证和JSON参数

时间:2021-10-24 19:33:17

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

我想为资源的REST接口提供这样的方法

@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.

但是,这是不可能的,因为我得到一个错误,如:“严重:缺少方法public void com.restaurant.FoodResource.add(com.restaurant.User,com.restaurant.Food)的依赖关系在索引0处的参数:SEVERE:Missing在索引1处严重依赖于方法public void com.restaurant.FoodResource.add(com.restaurant.User,com.restaurant.Food)在参数:方法,公共无效com.restaurant.FoodResource.add(com.restaurant.User, com.restaurant.Food),用资源的POST注释,类com.restaurant.FoodResource,不被认为是有效的资源方法。

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

我发现根据规范(3.3.2.1实体参数),不可能有两个没有注释某些注释的参数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.

还有什么选择可以做这样的事情?我想到的是一个封装User和Food的对象,但这似乎不是一个很好的解决方案。

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

有没有其他更优雅的方式来做这样的事情?我可以将JSON Food表示发送为@PathParam吗?

3 个解决方案

#1


3  

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.

在setUpResources()方法的资源单元测试中,使用addProvider()方法设置身份验证提供程序。

For example:

例如:

addProvider(new BasicAuthProvider(<Authenticator>, <CacheBuilderSpec>));

#2


0  

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.

因此,解决方法可能是添加未使用的@QueryParam或@Context参数。

#3


0  

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)

#1


3  

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.

在setUpResources()方法的资源单元测试中,使用addProvider()方法设置身份验证提供程序。

For example:

例如:

addProvider(new BasicAuthProvider(<Authenticator>, <CacheBuilderSpec>));

#2


0  

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.

因此,解决方法可能是添加未使用的@QueryParam或@Context参数。

#3


0  

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)