学习注解的使用模拟RequestMapping解析path

文章目录


前言

注解在后端开发过程中提供了许多的便利,提高了代码简洁性和可读性,在应用程序中占据越来越重要的作用,很有学习的必要,接下来会通过代码来完成对类、方法、属性注解的解析。


一、代码部分

java 复制代码
package test;

import com.student.SpringbootStart;
import com.student.controller.StudentCurriculumController;
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.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * Create by zjg on 2023/7/16
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootStart.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AnnotationTest {
    @Autowired
    private ConfigurableApplicationContext applicationContext;
    @Autowired
    private StudentCurriculumController studentCurriculumController;
    @Test
    public void main() throws NoSuchMethodException {
        //模拟RequestMapping路径的拼接
        //todo class
        Map<String, Object> annotations = applicationContext.getBeansWithAnnotation(RequestMapping.class);
        annotations.forEach((k,v)->{
            System.out.println(String.format("服务{%s}可访问服务列表如下:",k));
            RequestMapping requestMapping = applicationContext.findAnnotationOnBean(k, RequestMapping.class);
            String prefix="";
            if(requestMapping!=null){
                prefix=requestMapping.value()[0];
            }
            //todo method
            Method[] methods;
            if(v.getClass().getName().contains("$$")){//处理代理类
                methods= v.getClass().getSuperclass().getDeclaredMethods();
            }else{
                methods = v.getClass().getDeclaredMethods();
            }
            for(Method method:methods){
                RequestMapping requestMapping1 = method.getAnnotation(RequestMapping.class);
                String suffix="";;
                if(requestMapping1!=null){
                    suffix=requestMapping1.value().length==0?"":requestMapping1.value()[0];
                }
                PostMapping postMapping = method.getAnnotation(PostMapping.class);
                if(postMapping!=null){
                    suffix=postMapping.value()[0];
                }
                GetMapping getMapping = method.getAnnotation(GetMapping.class);
                if(getMapping!=null){
                    suffix=getMapping.value()[0];
                }
                if(!(prefix.endsWith("/"))&&!(suffix.startsWith("/"))){//此处是因为编码不规范,emo
                    suffix="/"+suffix;
                }
                System.out.println(prefix+suffix);
            }
            //todo field
            Field[] declaredFields;
            if(v.getClass().getName().contains("$$")){//处理代理类
                declaredFields= v.getClass().getSuperclass().getDeclaredFields();
            }else{
                declaredFields= v.getClass().getDeclaredFields();
            }
            for (Field declaredField : declaredFields) {
                Autowired autowired = declaredField.getAnnotation(Autowired.class);
                if(autowired!=null){
                    boolean required = autowired.required();
                    System.out.println(declaredField.getName()+".required="+required);
                }
            }
        });

    }
}

总结

回到顶部
代理类会使我们获取注解失败,要做特殊处理。

相关推荐
DKPT4 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
好好研究7 小时前
学习栈和队列的插入和删除操作
数据结构·学习
新中地GIS开发老师8 小时前
新发布:26考研院校和专业大纲
学习·考研·arcgis·大学生·遥感·gis开发·地理信息科学
SH11HF8 小时前
小菜狗的云计算之旅,学习了解rsync+sersync实现数据实时同步(详细操作步骤)
学习·云计算
Frank学习路上8 小时前
【IOS】XCode创建firstapp并运行(成为IOS开发者)
开发语言·学习·ios·cocoa·xcode
Chef_Chen10 小时前
从0开始学习计算机视觉--Day07--神经网络
神经网络·学习·计算机视觉
X_StarX12 小时前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生
MingYue_SSS12 小时前
开关电源抄板学习
经验分享·笔记·嵌入式硬件·学习
weixin_4373982112 小时前
转Go学习笔记(2)进阶
服务器·笔记·后端·学习·架构·golang
慕y27413 小时前
Java学习第十六部分——JUnit框架
java·开发语言·学习