Special Expression Support in Annotation In Dropwizard

15 Views Asked by At

I have an application developed Using Dropwizard framework.I have created an custom Annotation lets say "RateLimited" which has 2 argument lets say "limit" and "ttl" . Now I want to set this "limit" and "ttl" using an environment value defined in Config.yml

Below in my config.yml

limit : 10
ttl : 15

Below is configuration class

@Getter
class CustomConfiguration extends io.dropwizard.core.confiugation{
  private long limit;
  private long ttl;
}

Below is the implementation of annotation 'RateLimited'

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.NameBinding;

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE_USE})
public @interface RateLimited {
    long limit() default 0L;

    long ttl() default 0L;

    TimeUnit timeUnit() default TimeUnit.SECONDS;
}

below is my resource class

@Path("/employees")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
class MyResource{

    @GET
    @Path("/{id}")
    @RateLimited(limit = "${customConfiguration.getLimit()}" , ttl = "${customConfiguration.getTTl()}")
    public Employee getEmployees(@PathParam("id") Integer id){
        return employeeDao.findById(id).get();
    }
}

But it is not working, it is saying annotation expects constant value. Can anyone help me on this

0

There are 0 best solutions below