How to use reflection to identify annotated methods?
<p>We are trying to use reflection to extract the exact mapped values to our methods. The code structure in the target module consists of multiple classes as follows - </p>

<pre><code>@Controller
@RequestMapping(value = "/questions")
public class XYZController {

  @RequestMapping(value = "/ask")
  public boolean myMethod1() {..}

  @RequestMapping(value = "/{questionNo.}/{questionTitle}")
  public MyReturnObject myMethod2(){..}

}
</code></pre>

<p>What we are trying to grep here is the list of endpoints like <code>/questions/ask</code> and <code>/questions/{questionNo.}/{questionTitle}</code> for which the code we tried to execute filters all such classes based on the <code>@Controller</code> annotation and at the same time we are able to get the list of all the endpoints separately. The code we have tried so far is  - </p>

<pre><code>public class ReflectApi {

    public static void main(String[] args) {
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true);
        final List&lt;String&gt; filteredClasses = new ArrayList&lt;&gt;();
        scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
        Set&lt;BeanDefinition&gt; filteredPackage = scanner.findCandidateComponents("com.package.test");

        // gives me a list of all the filtered classes
        filteredPackage.stream().forEach(beanDefinition -&gt; filteredClasses.add(beanDefinition.getBeanClassName()));

        // call to get the annotation details
        filteredClasses.stream().forEach(filteredClass -&gt; getEndPoints(filteredClass));
    }

    public static void getEndPoints(String controllerClassName) {
        try {
            Class clazz = Class.forName(controllerClassName);
            Annotation classAnnotation = clazz.getDeclaredAnnotation(RequestMapping.class);
            if (classAnnotation != null) {
                RequestMapping mappedValue = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
                System.out.println("Controller Mapped -&gt; " + mappedValue.value()[0]); //This gives me the value for class like "/questions"
                runAllAnnotatedWith(RequestMapping.class);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // followed from a SO reference
    public static void runAllAnnotatedWith(Class&lt;? extends Annotation&gt; annotation) {
        Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forJavaClassPath())
                .setScanners(new MethodAnnotationsScanner()));
        Set&lt;Method&gt; methods = reflections.getMethodsAnnotatedWith(annotation);

        methods.stream().forEach(m -&gt; {
            if (m != null) {
                RequestMapping mappedValue = m.getAnnotation(RequestMapping.class); //this is listing out all the @RequestMapping annotations like "/questions" , "/ask" etc.
                    System.out.println(mappedValue.value()[0]);
            }
        });
    }
}
</code></pre>

<p><strong>But the missing part is the concatenation of the class to method RequestMapping value here.</strong></p>

<p>How do we loop inside a class to search annotations only from its methods? </p>

<p>Or is there any simpler way of doing this from what we are using?</p>

<p>References used - <a href="https://stackoverflow.com/questions/259140/scanning-java-annotations-at-runtime">Scanning Java annotations at runtime</a> &amp;&amp; <a href="https://stackoverflow.com/questions/16996033/how-to-run-all-methods-with-a-given-annotation">How to run all methods with a given annotation?</a></p>