简体   繁体   中英

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. I know jersey supports bean validation so i tried to use @NotNull annotation but it didn't work. 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.

After reading the docs i thought maybe dropwizard's configuration would be the reason. So i added this piece of code into my application's run method:

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对象而不是原始类型?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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