How can I create a dynamic proxy in java that retains parameter annotations on methods?

前端 未结 3 1105
一整个雨季
一整个雨季 2021-02-04 09:03

I currently am trying to proxy some existing JAX/RS resources, in order to allow me to use the Hibernate Validator\'s method validation support. However, when I proxy my class (

3条回答
  •  感情败类
    2021-02-04 09:46

    CGLib Enhancer technically is just extending from your Class. I do not know if this is feasible for you (number of objects) but how about exposing an interface instead of the class?

    Object p2 = Enhancer.create(resource.getClass(),
        new Class[] { IResource.class },
        new MethodInterceptor() {
          public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3)
              throws Throwable {
                return arg3.invokeSuper(arg0, arg2);
               }
        });
    

    Remove the annotations from the enhanced class and put them into the interface. Validate then against the interface. This might be a lot of boiler-plate interfaces for many such resources, but still seams a lot better than mapping everything to form backing objects or DTOs.

提交回复
热议问题