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

    }
}

总结

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

相关推荐
deng-c-f26 分钟前
Linux C/C++ 学习日记(34):协程(四):服务器向Mysql请求数据的三种编程方式:同步、线程、协程
学习
摸鱼仙人~40 分钟前
一文深入学习Java动态代理-JDK动态代理和CGLIB
java·开发语言·学习
敲敲了个代码2 小时前
为什么 Electron 项目推荐使用 Monorepo 架构 [特殊字符][特殊字符][特殊字符]
前端·javascript·学习·架构·electron·github
俊俊谢2 小时前
【序章】金融量化入门级学习——暨一颗韭菜的茁壮成长
学习·金融
Ro Jace2 小时前
“透彻式学习”与“渗透式学习”
学习
✎ ﹏梦醒͜ღ҉繁华落℘2 小时前
freeRTOS学习笔记(十二)--信号量
笔记·学习
野老杂谈4 小时前
如何快速学习智能合约开发语言 Solidity
开发语言·学习·智能合约·solidity·以太坊·区块链开发
Han.miracle4 小时前
Java线程的学习—多线程(一)
java·开发语言·学习
忧郁奔向冷的天4 小时前
视觉SLAM十四讲2nd—学习笔记(二)20250817
笔记·学习
立志成为大牛的小牛5 小时前
数据结构——三十一、最小生成树(王道408)
数据结构·学习·程序人生·考研·算法