学习注解的使用模拟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);
                }
            }
        });

    }
}

总结

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

相关推荐
_李小白14 分钟前
【OSG学习笔记】Day 24: Texture2D 与 Image
android·笔记·学习
笨小古1 小时前
VLA学习笔记——持续更新中
学习·机器人·大模型·具身智能·vla
solicitous1 小时前
遇到一个口头机遇的答辩准备2(ai告诉的要点)
学习·生活
青衫码上行1 小时前
【从零开始学习JVM】内存模型+堆栈的区别
java·jvm·学习·面试
艾莉丝努力练剑1 小时前
【Linux线程】Linux系统多线程(二):线程的优缺点
linux·运维·服务器·c++·学习
艾莉丝努力练剑1 小时前
【Linux线程】Linux系统多线程(一):线程概念
java·linux·运维·服务器·开发语言·学习·线程
嵌入式小企鹅1 小时前
Claude开源风暴?半导体设备突破?
大数据·人工智能·学习·开源·嵌入式·半导体·ai芯片
2501_920627612 小时前
Flutter 框架跨平台鸿蒙开发 - 数学学习助手
学习·flutter·华为·harmonyos
如雨随行20202 小时前
【Vim】学习笔记(10)tips-3
笔记·学习·vim
少许极端2 小时前
算法奇妙屋(三十九)-贪心算法学习之路 6
java·学习·算法·贪心算法