Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I created a little sample project out of my own curiosity and fiddled a little with the MetadataReader provided by Spring. For the demo I created a very simple controller which looked like this:</p> <pre><code>@Controller public class SomeAnnotatedController { @RequestMapping(method = {RequestMethod.GET}, value = "/someUrl") public void someMethod() { // do something later } } </code></pre> <p>I was not able to extract the correct information from the annotation using the Spring MetadataReader.</p> <pre><code>@Test public void shouldReturnMethodArrayWithSpringMetadataReader() throws Exception { MetadataReader metadataReader = new CachingMetadataReaderFactory().getMetadataReader(SomeAnnotatedController.class.getName()); Set&lt;MethodMetadata&gt; annotatedMethods = metadataReader.getAnnotationMetadata().getAnnotatedMethods(RequestMapping.class.getName()); assertEquals(1, annotatedMethods.size()); MethodMetadata methodMetadata = annotatedMethods.iterator().next(); assertEquals("someMethod", methodMetadata.getMethodName()); Map&lt;String, Object&gt; annotationAttributes = methodMetadata.getAnnotationAttributes(RequestMapping.class.getName()); assertTrue(annotationAttributes.containsKey("method")); RequestMethod[] methodAttribute = (RequestMethod[]) annotationAttributes.get("method"); assertEquals(1, methodAttribute.length); } </code></pre> <p>Running this test fails in the last line and tells you that this is an empty array...</p> <pre><code>java.lang.AssertionError: Expected :1 Actual :0 </code></pre> <p>Doing the same with native Java feels a little bit easier and returns the correct information.</p> <pre><code>@Test public void shouldReturnMethodArrayWithPlainJava() throws Exception { Method method = SomeAnnotatedController.class.getDeclaredMethod("someMethod"); RequestMapping annotation = method.getAnnotation(RequestMapping.class); assertEquals(1, annotation.method().length); assertEquals(RequestMethod.GET, annotation.method()[0]); } </code></pre> <p>So I am sorry to tell you that I did not find a solution to the problem but maybe the sample project or the documented alternative based on plain java might help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload