简体   繁体   English

jersey-dropwizard上的资源查询参数验证

[英]Resource query parameter validation on jersey-dropwizard

I have a dropwizard application and in one resource i would like to have a required query parameter. 我有一个dropwizard应用程序,并且在一种资源中,我想要一个必需的查询参数。 I know jersey supports bean validation so i tried to use @NotNull annotation but it didn't work. 我知道jersey支持Bean验证,所以我尝试使用@NotNull批注,但没有用。 Here's the code to resource: 这是资源代码:

@Path("/resource/{id}")
@Produces(MediaType.APPLICATION_JSON)
public class MyResource {
    //....constructor...

    @GET
    public String test(
        @PathParam("id") long id,
        @NotNull @QueryParam("required_param") long param) {
        //....
    }
}

In case it isn't clear, what i want to happen is whenever the client sends a request without the parameter required_param i want to return an error. 如果不清楚,我想发生的事情是每当客户端发送不带参数required_pa​​ram的请求时,我都想返回一个错误。

After reading the docs i thought maybe dropwizard's configuration would be the reason. 阅读文档后,我认为也许dropwizard的配置是原因。 So i added this piece of code into my application's run method: 因此,我将这段代码添加到了我的应用程序的run方法中:

environment.jersey().property("jersey.config.disableAutoDiscovery", false);
environment.jersey().property("jersey.config.server.disableAutoDiscovery", false);
environment.jersey().property("jersey.config.beanValidation.disable.server", false);

Unfortunately this didn't work either. 不幸的是,这也不起作用。 Anyone has an idea on how to do this? 有人对如何执行此操作有想法吗?

您是否尝试过使用Long对象而不是原始类型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM