• RequestMappingHandlerMapping类的简介说明


    转自:

    RequestMappingHandlerMapping类的简介说明

    下文笔者将讲述RequestMappingHandlerMapping类的简介说明,如下所示:

    RequestMappingHandlerMapping的功能:
       RequestMappingHandlerMapping是AbstractHandlerMethodMapping唯一的实现类
         它根据@RequestMapping注解生成 RequestMappingInfo,同时提供isHandler实现。
    

    afterPropertiesSet() 重写父类初始化方法

    @Override
    public void afterPropertiesSet() {
    	//config为RequestMappingInfo的创建对象
    	this.config = new RequestMappingInfo.BuilderConfiguration();
    	//设置urlPathHelper默认为UrlPathHelper.class
    	this.config.setUrlPathHelper(getUrlPathHelper());
    	//默认为AntPathMatcher,路径匹配校验器
    	this.config.setPathMatcher(getPathMatcher());
    	//是否支持后缀补充,默认为true
    	this.config.setSuffixPatternMatch(this.useSuffixPatternMatch);
    	//是否添加"/"后缀,默认为true
    	this.config.setTrailingSlashMatch(this.useTrailingSlashMatch);
    	//是否采用mediaType匹配模式,比如.json/.xml模式的匹配,默认为false      
    	this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
    	//mediaType处理类      
    	this.config.setContentNegotiationManager(getContentNegotiationManager());
    	//调用父类进行HandlerMethod的注册工作
    	super.afterPropertiesSet();
    }
    

    isHandler() 判断获取何种类型的Handler

    @Override
    protected boolean isHandler(Class beanType) {
    	//优先匹配@Controller
    	return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
    			AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
    }
    

    获取@Controller/@RequestMapping注解下的handler

    getMappingForMethod() 获取HandlerMethod对应的mapping属性
    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) {
    	//对method以及class类都进行创建RequestMappingInfo 
    	//因为@RequestMapping可以在方法上/类上应用注解
    	RequestMappingInfo info = null;
    	// 读取方法上的RequestMapping注解信息
    	RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
    	if (methodAnnotation != null) {
    	 // 读取自定义的条件,这边没有使用
    	 RequestCondition methodCondition = getCustomMethodCondition(method);
    	 // 根据方法上的RequsetMapping注解和自定义条件,生成匹配条件.这边的匹配条件包括http method,request parameter,request header等
    	 info = createRequestMappingInfo(methodAnnotation, methodCondition);
    	 // 读取类上的RequestMapping注解信息
    	 RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
    	 if (typeAnnotation != null) {
    		 RequestCondition typeCondition = getCustomTypeCondition(handlerType);
    		 // 生成类上的匹配条件,并合并方法上的
    		 info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info);
    	 }
    	}
    	return info;
    }
    protected RequestMappingInfo createRequestMappingInfo(RequestMapping requestMapping, RequestCondition customCondition) {
    	return RequestMappingInfo
    			.paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))
    			.methods(requestMapping.method())
    			.params(requestMapping.params())
    			.headers(requestMapping.headers())
    			.consumes(requestMapping.consumes())
    			.produces(requestMapping.produces())
    			.mappingName(requestMapping.name())
    			.customCondition(customCondition)
    			.options(this.config)
    			.build();
    }
  • 相关阅读:
    uniapp视频压缩踩坑记录
    自定义QChartView实现鼠标放在图表时,显示鼠标位置坐标值(x,y)
    UVA 11572 唯一的雪花 Unique Snowflakes
    java基于ssm的体育馆票务管理系统
    LeetCode27.移除元素
    Linux网络基础知识全面总结
    如何组装一个注册中心?
    放大器的稳定性分析举例
    如何访问TDH中Inceptor 底层的元数据库TxSQL
    深度学习-卷积神经网络-ResNET
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/127815788