Method 反射方法获取所有http方法映射

参考学习:https://blog.csdn.net/a123638/article/details/52909435

package com.dakun.jianzhong.config;

import com.dakun.jianzhong.client.MicroserviceApiService;
import com.dakun.jianzhong.utils.IsPublic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.*;

@Component
@Order(value = 1)
public class APIRegistor implements ApplicationRunner {
    @Resource
    private  ApplicationContext applicationContext;
    @Value("${spring.application.name}")
    private String serviceName;
    @Resource
    private MicroserviceApiService microserviceApiService;
    @Override
    public void run(ApplicationArguments args) throws Exception {

        int i =0;
        Map, Object> apies =new HashMap<>();// 存储所有url,IsPublic 集合
        WebApplicationContext wac = (WebApplicationContext) applicationContext;//获取上下文对象
        RequestMappingHandlerMapping bean = wac.getBean(RequestMappingHandlerMapping.class);//通过上下文对象获取RequestMappingHandlerMapping实例对象
        Map, HandlerMethod> handlerMethods = bean.getHandlerMethods();
        for (Map.Entry, HandlerMethod> entry : handlerMethods.entrySet()) {
            String baseUrls = "";
            //获取方法路径
            RequestMappingInfo key = entry.getKey();
            HandlerMethod value = entry.getValue();
            PatternsRequestCondition prc = key.getPatternsCondition();
            Set patterns = prc.getPatterns();//url个数,正常为1,可以读取多个:eg:@PostMapping({"/add","/edit"})
            Method method = value.getMethod();
            boolean is_public = false;
            if(method.isAnnotationPresent(IsPublic.class)){
                is_public = method.getAnnotation(IsPublic.class).value();
            }
            for (String pattern : patterns) {
                //所有url
                baseUrls = "/"+serviceName+pattern;
                apies.put(baseUrls,is_public);
                if(apies.size()>=100){
                    microserviceApiService.addApies(apies);
                    apies.clear();
                }
                i++;
            }

        }
        microserviceApiService.addApies(apies);
        if(!apies.isEmpty()){
            System.out.println("product-service apies size:"+i);
        }
/*
        Map map= applicationContext.getBeansWithAnnotation(RestController.class);
        //网关的Map
        Map apies =new HashMap<>();
        int i = 0;
       for(Map.Entry entry:map.entrySet()){
            Object controller = entry.getValue();
            //controller mapping path
            String basepath =  controller.getClass().getAnnotation(RequestMapping.class).value()[0];
            Method[] ms = controller.getClass().getDeclaredMethods();
            for(Method m:ms){
                String key = "";
                //获取方法路径
                String methodpath ="";
                boolean is_public = false;
                if(m.isAnnotationPresent(RequestMapping.class)){
                    methodpath =  m.getAnnotation(RequestMapping.class).value()[0];
                }else if(m.isAnnotationPresent(PostMapping.class)){
                    methodpath =  m.getAnnotation(PostMapping.class).value()[0];
                }
                else if(m.isAnnotationPresent(GetMapping.class)){
                    methodpath =  m.getAnnotation(GetMapping.class).value()[0];
                }
                key = "/"+serviceName+basepath+methodpath;
                if(m.isAnnotationPresent(IsPublic.class)){
                    is_public = m.getAnnotation(IsPublic.class).value();
                }
                apies.put(key,is_public);
                if(apies.size()>=100){
                    microserviceApiService.addApies(apies);
                    apies.clear();
                }
                i++;
            }
        }
        microserviceApiService.addApies(apies);
       if(!apies.isEmpty()){
           System.out.println("resource-service apies size:"+i);
       }*/
    }
}
 
  

通过两期的维护两种方式获取@RequestMapping,@PostMapping,@GetMapping注解的所有url地址


你可能感兴趣的:(基础知识)