How to create a custom annotation in Spring Boot and run it automatically when a method is called?

Asked

Viewed 77 times

0

I would like to create my own annotations. For example; when a controller spring is called and it has my annotation, some validation is executed.

@GetMapping
@MinhaAnotacao(value="log") // Aqui faria um log, com a request
public ResponseEntity<?> get(){
  ...
}

How could I do this in spring boot? I don’t have much experience with annotations in Java. You could create the annotation as in normal java with Reflection, but all the examples I found, the annotation was created and then to execute it you had to call a method at hand in the implementation to do some action with that method. How annotations like @pathVariable, @Component, @Service, @Bean are automatically called?

1 answer

0

For this log case, you could use @Aspect.

Basically you will create a custom Interceptor that will log in what you need on your @Getmapping input or output.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.