Home > Software design >  how to add specific logic to the method by custom annotation in Spring boot?
how to add specific logic to the method by custom annotation in Spring boot?

Time:11-11

My question is this.

I would like to add custom annotation to spring boot and designate it as declared without declaring a specific logic.

Suppose you have the following code:

@MyCustomAnnotation
@Controller
public class ExController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model){
        return "index";
    }
}

I want the above code to perform the following logic.

@MyCustomAnnotation
@Controller
public class ExController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model){
        
        //Code added because it has an annotation
        
           model.addAttribute("key","value");  

        //Code added because it has an annotation
        
        return "index";
    }
}

I've been thinking about Reflection and other methods, but I can't think of the right way

Can someone grateful give a solution or keyword for this problem?

CodePudding user response:

You can add specific logic to the method by custom annotation in Spring boot by using the @Component annotation.

CodePudding user response:

@RequestParam is a Spring annotation that is used to bind a parameter to the value of a method parameter. If you want to add logic to your method, you can do so without any annotations. public ResponseEntity<String> myMethod(@RequestParam(required = false) String param) { //additional logic return new ResponseEntity<>(HttpStatus.OK); } The above code will add additional logic to the method.

i hope this can be helpful i am new to this .

  • Related