Spring

spring MVC

DispatcherServlet #doDispatch

-> HandlerExecutionChain 
-> HandlerAdapter
-> HandlerExecutionChain.pre
-> HandlerAdapter.handle
-> HandlerExecutionChain.post

九大内置对象
HandlerMapping
HandlerAdapter
HandlerExceptionResolver
RequestToViewNamelator
ViewResolver
LocaleResolver
ThemeResolver
MultipartResolver
FlashMapManager
获取系统的所有url映射mapping方法
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.util.*;

@RestController
@RequestMapping("/test")
@Slf4j
public class MappingController {
    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @RequestMapping(value = "/mappings")
    public String list(Model model) {
        List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();
        List<String> list = new ArrayList<>();

        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            RequestMappingInfo info = m.getKey();
            HandlerMethod method = m.getValue();
            PatternsRequestCondition p = info.getPatternsCondition();
            for (String url : p.getPatterns()) {
                hashMap.put("url", url);
                list.add(url);
            }
            hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
            hashMap.put("method", method.getMethod().getName()); // 方法名
            RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
            String type = methodsCondition.toString();
            if (type != null && type.startsWith("[") && type.endsWith("]")) {
                type = type.substring(1, type.length() - 1);
                hashMap.put("type", type); // 方法名
            }
            urlList.add(hashMap);
        }
        model.addAttribute("list", urlList);

        list.forEach(a-> System.out.println(a));

        for (HashMap<String, String> hashMap : urlList) {
            hashMap.entrySet().stream().forEach(a->{
                System.out.println(a.getValue());
            });
            System.out.println();
        }

        return "/console/system/mappingList";
    }
}
posted @   rbcd  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示