I have a spring boot application with a custom filter:
import javax.servlet.Filter
public class MyFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
System.out.println("MyFilter");
}
}
Some controllers in the application have custom annotations over their methods. All custom annotations implement the myAnnotation interface.
@GetMapping("/")
@MyCustomAnnotation
@AnotherCustomAnnotation
public ResponseEntity<String> get(){
addSomeMetric();
return new ResponseEntity<>("Hello world", HttpStatus.OK);
}
How can get i get a list of all annotations defined on the URI endpoint from within the doFilter code?
Spring AOP can be used to decide if a controller method need to be executed based on request/user.
Following code is one way of implementing the same.
Also note that the annotation RetentionPolicy must be RUNTIME , for this code to work.