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

    }
}

总结

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

相关推荐
MartinYeung519 分钟前
[论文学习]AgentSafetyBench:大语言模型智能体安全评估基准深度分析
学习·安全·语言模型
艾莉丝努力练剑2 小时前
【Linux:动静态库】Linux 动静态库与可执行文件
linux·运维·服务器·学习·面试·文件系统·动静态库
宁风NF2 小时前
JavaScript:内存、垃圾回收、性能优化
开发语言·前端·javascript·学习·性能优化·es6
编程圈子3 小时前
电机驱动开发学习22. 霍尔电角度标定(自动/手动标定与Flash固化)
驱动开发·学习
m4Rk_3 小时前
【论文阅读】Agent 记忆机制(26):Infini Memory——将长期记忆维护成可读写的主题文档
论文阅读·人工智能·学习·开源·github
正经人_x4 小时前
学习日记46:LISA: Reasoning Segmentation via Large Language Model
人工智能·学习·语言模型
神龙天舞20015 小时前
CMake构建学习笔记-通用的CMake构建脚本
笔记·学习
一只小菜鸡..6 小时前
南京大学 操作系统 (JYY) 学习笔记:并发 Bug 的地狱——数据竞争、死锁与原子性违反
笔记·学习·bug
似的83513 小时前
一步一步学习使用FireMonkey动画() 使用TAnimator类创建动画
linux·学习·nginx
fanged14 小时前
总线学习3--SPI番外(TODO)
学习