使用Spring@AliasFor注解自定义注解合并@Controller和@RequestMapping
如果只需要代码可以直接根据目录进行获取
知识点
魔法本源先附上官方文档
然后开始扒,作为一代初级程序员恐怖如斯…码来
码
-
自定义注解 package com.ruoyi.common.annotation;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Controller
@RequestMapping
@Documented
public @interface ControllerMapping {
@AliasFor(annotation = Controller.class, value = "value")
String controllerValue() default "";
@AliasFor(annotation = RequestMapping.class)
String name() default "";
@AliasFor(annotation = RequestMapping.class, value = "value")
String[] requestMappingValue() default {};
@AliasFor(annotation = RequestMapping.class)
String[] path() default {};
@AliasFor(annotation = RequestMapping.class)
RequestMethod[] method() default {};
@AliasFor(annotation = RequestMapping.class)
String[] params() default {};
@AliasFor(annotation = RequestMapping.class)
String[] headers() default {};
@AliasFor(annotation = RequestMapping.class)
String[] consumes() default {};
@AliasFor(annotation = RequestMapping.class)
String[] produces() default {};
}
-
测试代码 @ControllerMapping(requestMappingValue = "/test")
public class TestAnnotationController {
@GetMapping
@ResponseBody
public String treeData() {
return "Hello-World";
}
}
-
结果 C:\Users\Admin>curl -X GET 127.0.0.1/test
Hello-World
同理可以合并@Autowired和@Qualifier
|