BAEL-4791: Spring AliasFor Annotation (#10794)
This commit is contained in:
parent
5e68c55f06
commit
26f3376b1b
@ -19,6 +19,11 @@
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
@ -0,0 +1,29 @@
|
||||
package com.baeldung.aliasfor;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@RequestMapping
|
||||
public @interface MyMapping {
|
||||
|
||||
@AliasFor(annotation = RequestMapping.class, attribute = "method")
|
||||
RequestMethod[] action() default {};
|
||||
|
||||
@AliasFor(annotation = RequestMapping.class, attribute = "path")
|
||||
String[] value() default {};
|
||||
|
||||
@AliasFor(annotation = RequestMapping.class, attribute = "path")
|
||||
String[] mapping() default {};
|
||||
|
||||
@AliasFor(annotation = RequestMapping.class, attribute = "path")
|
||||
String[] route() default {};
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.aliasfor;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
public class MyMappingController {
|
||||
|
||||
@MyMapping(action = RequestMethod.PATCH, route = "/test")
|
||||
public void mappingMethod() {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.baeldung.aliasfor;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = MyMappingController.class)
|
||||
public class AliasForUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
Class controllerClass;
|
||||
|
||||
@Before
|
||||
public void setControllerBean() {
|
||||
MyMappingController controllerBean = context.getBean(MyMappingController.class);
|
||||
controllerClass = controllerBean.getClass();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenComposedAnnotation_whenExplicitAlias_thenMetaAnnotationAttributeOverridden() {
|
||||
|
||||
for (Method method : controllerClass.getMethods()) {
|
||||
if (method.isAnnotationPresent(MyMapping.class)) {
|
||||
MyMapping annotation = AnnotationUtils.findAnnotation(method, MyMapping.class);
|
||||
RequestMapping metaAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
|
||||
|
||||
assertEquals(RequestMethod.PATCH, annotation.action()[0]);
|
||||
|
||||
assertEquals(0, metaAnnotation.method().length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenComposedAnnotation_whenImplictAlias_thenAttributesEqual() {
|
||||
for (Method method : controllerClass.getMethods()) {
|
||||
if (method.isAnnotationPresent(MyMapping.class)) {
|
||||
MyMapping annotationOnBean = AnnotationUtils.findAnnotation(method, MyMapping.class);
|
||||
|
||||
assertTrue(annotationOnBean.mapping()[0].equals(annotationOnBean.route()[0]));
|
||||
assertTrue(annotationOnBean.value()[0].equals(annotationOnBean.route()[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user