0

I have a controller class

@RestController
public class Controller {

    @GetMapping("endpoint")
    public void endpoint() {
    }
}

and need the reference to the method handling the request i.e. Controller#endpoint().

I'm not looking for obtaining a reference to the current method but rather assume I'm in a filter or service for example and I need a reference to the endpoint method.

(The ultimate goal is to then assess wheter @SomeRelevantAnnotation is present or not deep inside my custom spring security stack but that's out of scope for this question.)

I think I could use the RequestContextHolder instance which embedds the request instance that I can then use in HandlerMapping#getHandler(request) to get a reference to the method.

Is there an easier way?

Emanuel George Hategan
  • 1,123
  • 1
  • 13
  • 22
  • What do you mean by "reference" ? The name of the method ? – Lutzi May 24 '18 at 16:10
  • By reference I mean a java reference, a variable to the Method instance in order to then use reflection. – Emanuel George Hategan May 24 '18 at 16:14
  • Did you take a look to that https://stackoverflow.com/questions/442747/getting-the-name-of-the-currently-executing-method?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa ? There are multiples good answers. – Lutzi May 24 '18 at 16:16
  • I'm not looking for the currently executing method but rather assume I'm somewhere in a Filter for example and I need a reference to the endpoint method. – Emanuel George Hategan May 24 '18 at 16:29
  • @jim-garrison I'm not interested in getting a reference to the current method. I edited the question. It is not a duplicate question. – Emanuel George Hategan May 24 '18 at 16:33
  • The filter would be invoked BEFORE the handling method. There's no way to know what is going to be eventually invoked. Needing a reference to the handler sounds like you may need a different design. Also, by "reference to method" do you mean a reflection `Method` object? – Jim Garrison May 24 '18 at 16:35
  • The controller is a Spring bean. All these annotations ultimately get translated into the controller's bean definition. So the method reference must be in the Spring app context somewhere. That said, I kind of agree with @JimGarrison that this idea itself may be worth reconsidering. – jingx May 24 '18 at 16:49
  • @JimGarrison yes I meant a reflection Method object. It may be the wrong design as you suggest. I'll reconsider my approach and rephrase this as a new question more in line with the end goal. – Emanuel George Hategan May 25 '18 at 09:57

0 Answers0