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

    }
}

总结

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

相关推荐
摸鱼仙人~28 分钟前
简单的GAN生成学习案例
人工智能·学习·生成对抗网络
开放知识图谱43 分钟前
论文浅尝 | G2S:一个用于大语言模型的时间知识图预测的通用到具体的学习框架(ACL2025)
人工智能·学习·语言模型·自然语言处理
好奇龙猫1 小时前
【人工智能学习-AI-MIT公开课-第二节-推理:目标树与问题求解(方法)】
学习
丝斯20111 小时前
AI学习笔记整理(36)——自然语言处理
人工智能·笔记·学习
好奇龙猫1 小时前
【AI学习-comfyUI学习-第二十二-DepthAnythingV2深度图工作流-各个部分学习】
人工智能·学习
stars-he2 小时前
单相双半波可控整流电路的MATLAB仿真设计
笔记·学习·matlab
龙亘川2 小时前
AI 赋能智慧农业:核心技术、应用案例与学习路径全解析
人工智能·学习
im_AMBER2 小时前
Leetcode 87 等价多米诺骨牌对的数量
数据结构·笔记·学习·算法·leetcode
好奇龙猫2 小时前
【AI学习-comfyUI学习-第二十一-LMSD线段预处理器(建筑概念设计图)-各个部分学习】
人工智能·学习
dog2502 小时前
基于历史学习的拥塞控制算法
学习·拥塞控制