如何使用带有dropwizard的自定义验证器?

Shu*_*rya 1 java validation annotations jersey dropwizard

我有一个由其他人编写的REST api,其中处理对特定url的请求的方法接受从路径参数填充的一堆参数.

@POST
@Path("/{classid}/{studentid}/details")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@SuppressWarnings("unchecked")


public Response processFile(@FormDataParam("sourceFile") InputStream aStream, @PathParam("classid") String classId, @PathParam("studentid")  String studentId, @Context HttpServletRequest httpRequest) {

// Code to do stuff and return a response
}
Run Code Online (Sandbox Code Playgroud)

写这篇文章的人使用了DropWizard,我之前没有使用它的经验.我的任务是通过将studentId字段与db中的值进行比较来验证studentId字段.这将是非常简单的,但我被告知使用自定义验证器.我对编写注释很新,但经过多次挖掘写了这样的注释,

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = StudentIdValidator.StudentIdValidation.class)

public @interface StudentIdValidator {

    String message() default "{Invalid Id}";

      Class<?>[] groups() default {};

      Class<? extends Payload>[] payload() default {};


      class StudentIdValidation implements ConstraintValidator<StudentIdValidator, String> {

        @Override
        public void initialize(StudentIdValidator constraintAnnotation) {
            System.out.println("Annotation initialize !!!!!!!!!");
        }


        @Override
        public boolean isValid(String value, ConstraintValidatorContext context)                                                                            {
                // TODO Auto-generated method stub
                System.out.println("Annotation called");
                return false;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在此之后,我将注释添加到我想要运行验证的字段,就像这样,

public Response processFile(@FormDataParam("sourceFile") InputStream     aStream, @PathParam("classid") String classId, @StudentIdValidator     @PathParam("studentid")  String studentId, @Context HttpServletRequest     httpRequest)
Run Code Online (Sandbox Code Playgroud)

现在的问题是,当我运行/调试代码时...这个验证器没有被调用,我也不知道如何在studentId验证类中获取studentId的值.所以我挖了一些,并将其添加到应用程序文件中

class MyApplication extends Application<MyConfiguration> {
    ........

    @Override
   public void run(MyConfiguration myConfiguration, Environment                     currentEnvironment) {

     currentEnvironment.jersey().register(StudentIdValidator.class);

    }
Run Code Online (Sandbox Code Playgroud)

我确实在智慧的尽头.任何帮助将非常非常感激.抱歉格式不佳.

pan*_*adb 5

这很简单.我会在这里粘贴我的例子,因为我写了它,我很懒,不想带走你的乐趣:)

编辑:我认为您的问题是您没有使用@Valid注释您的资源

所以我们走了:

您使用验证器走在正确的轨道上.这些是我的:

public class CustomValidator implements ConstraintValidator<CustomValidation, String> {

    @Override
    public void initialize(CustomValidation constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        System.out.println("Validation called");

        return false;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是注释:

@Constraint(validatedBy = {CustomValidator.class})
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface CustomValidation {


      String message() default "Some message";

      Class<?>[] groups() default {};
      Class<? extends Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)

应用程序:

public class Application extends io.dropwizard.Application<Configuration>{

    @Override
    public void run(Configuration configuration, Environment environment) throws Exception {
        MetricRegistry metrics = environment.metrics();
        environment.jersey().register(new HelloResource(metrics));

    }

    public static void main(String[] args) throws Exception {
        new Application().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml");
    }
}
Run Code Online (Sandbox Code Playgroud)

和资源:

@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class HelloResource {

    private MetricRegistry service;

    public HelloResource(MetricRegistry service) {
        this.service = service;
    }

    @GET
    public String hello() {

        Timer timer = service.timer("test");

        try(Context t = timer.time()) {
            return "Hello World";
        }

    }


    @GET
    @Path("/test2")
    public void test(@Valid @CustomValidation @QueryParam("arg") String test) {
        System.out.println(test);
    }
}
Run Code Online (Sandbox Code Playgroud)

不介意指标,它们与它无关.重要的是你需要告诉DW你想验证什么.

在资源中,请参阅测试方法.我用@Valid注释我需要的参数(告诉DW验证)@CustomValidation(告诉DW使用什么验证器).

这实际上不是Dropwizard功能,而是一个hibernate验证器实现.

它的工作方式是hibernate通过调用构造函数在请求时动态创建Validator类.现在,如果你有简单的验证(比如比较一个字符串),这个工作非常好.如果你需要依赖关系,那么它会变得更加棘手.我也有一个例子,你可以在这里阅读:

使用dropwizard验证,我可以访问数据库以插入记录

这个例子使用了guice,但它演示了如何将自己的创建机制挂钩到验证中.这样,您可以控制验证器创建,并使用数据源注入或初始化它们以访问您的数据库.

我希望能回答你的问题,

阿图尔