首页 > 其他分享 >Spring mvc之RequestMappingInfo类

Spring mvc之RequestMappingInfo类

时间:2023-12-12 14:07:06浏览次数:27  
标签:return Nullable Spring request private RequestMappingInfo mvc null

Spring mvc之RequestMappingInfo类_ide

请求映射信息。封装以下请求映射条件:

  1. PatternsRequestCondition
  2. RequestMethodsRequestCondition
  3. ParamsRequestCondition
  4. HeadersRequestCondition
  5. ConsumesRequestCondition
  6. ProducesRequestCondition
  7. RequestCondition (optional, custom request condition)

1. 参数说明

@Nullable
    private final String name;

    private final PatternsRequestCondition patternsCondition;

    private final RequestMethodsRequestCondition methodsCondition;

    private final ParamsRequestCondition paramsCondition;

    private final HeadersRequestCondition headersCondition;

    private final ConsumesRequestCondition consumesCondition;

    private final ProducesRequestCondition producesCondition;

    private final RequestConditionHolder customConditionHolder;

    private final int hashCode;

1.1 属性name

这个映射对象的名字

1.2 属性patternsCondition

此RequestMappingInfo对象的URL模式

Spring mvc之RequestMappingInfo类_构造方法_02

如图所示该对象的patterns属性是一个Set接口实现类,保存了当前请求访问路径。

1.3 属性methodsCondition

此RequestMappingInfo的HTTP请求方法;

Spring mvc之RequestMappingInfo类_构造方法_03

该属性methods集合中存储了请求类型的枚举信息。

1.4 属性paramsCondition

此RequestMappingInfo的“参数”条件

1.5 属性headersCondition

此RequestMappingInfo的“headers”条件;

1.6 属性consumesCondition

1.7 属性customConditionHolder

2. 方法说明

构造方法有3个,如下所示:

public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondition patterns,
            @Nullable RequestMethodsRequestCondition methods, @Nullable ParamsRequestCondition params,
            @Nullable HeadersRequestCondition headers, @Nullable ConsumesRequestCondition consumes,
            @Nullable ProducesRequestCondition produces, @Nullable RequestCondition<?> custom) ;

public RequestMappingInfo(@Nullable PatternsRequestCondition patterns,
            @Nullable RequestMethodsRequestCondition methods, @Nullable ParamsRequestCondition params,
            @Nullable HeadersRequestCondition headers, @Nullable ConsumesRequestCondition consumes,
            @Nullable ProducesRequestCondition produces, @Nullable RequestCondition<?> custom);

public RequestMappingInfo(RequestMappingInfo info, @Nullable RequestCondition<?> customRequestCondition);

完整的构造方法业务代码如下:

public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondition patterns,
            @Nullable RequestMethodsRequestCondition methods, @Nullable ParamsRequestCondition params,
            @Nullable HeadersRequestCondition headers, @Nullable ConsumesRequestCondition consumes,
            @Nullable ProducesRequestCondition produces, @Nullable RequestCondition<?> custom) {

        this.name = (StringUtils.hasText(name) ? name : null);
        this.patternsCondition = (patterns != null ? patterns : EMPTY_PATTERNS);
        this.methodsCondition = (methods != null ? methods : EMPTY_REQUEST_METHODS);
        this.paramsCondition = (params != null ? params : EMPTY_PARAMS);
        this.headersCondition = (headers != null ? headers : EMPTY_HEADERS);
        this.consumesCondition = (consumes != null ? consumes : EMPTY_CONSUMES);
        this.producesCondition = (produces != null ? produces : EMPTY_PRODUCES);
        this.customConditionHolder = (custom != null ? new RequestConditionHolder(custom) : EMPTY_CUSTOM);

        this.hashCode = calculateHashCode(
                this.patternsCondition, this.methodsCondition, this.paramsCondition, this.headersCondition,
                this.consumesCondition, this.producesCondition, this.customConditionHolder);
    }

3. 匹配

3.1 匹配调用栈

AbstractHandlerMethodMapping方法

private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {
        for (T mapping : mappings) {
            T match = getMatchingMapping(mapping, request);
            if (match != null) {
                matches.add(new Match(match, this.mappingRegistry.getMappings().get(mapping)));
            }
        }
    }

RequestMappingInfoHandlerMapping类方法

@Override
    protected RequestMappingInfo getMatchingMapping(RequestMappingInfo info, HttpServletRequest request) {
        return info.getMatchingCondition(request);
    }

3.2 匹配业务具体实现

检查此请求映射信息中的所有条件是否与提供的请求匹配,并返回一个可能的新请求映射信息,其中包含为当前请求定制的条件。
例如,返回的实例可能包含与当前请求匹配的URL模式的子集,并在顶部按最佳匹配模式排序。
返回:如果匹配,则为新实例;否则为空。

@Override
    @Nullable
    public RequestMappingInfo getMatchingCondition(HttpServletRequest request) {
        RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request);
        if (methods == null) {
            return null;
        }
        ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request);
        if (params == null) {
            return null;
        }
        HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request);
        if (headers == null) {
            return null;
        }
        ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request);
        if (consumes == null) {
            return null;
        }
        ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request);
        if (produces == null) {
            return null;
        }
        PathPatternsRequestCondition pathPatterns = null;
        if (this.pathPatternsCondition != null) {
            pathPatterns = this.pathPatternsCondition.getMatchingCondition(request);
            if (pathPatterns == null) {
                return null;
            }
        }
        PatternsRequestCondition patterns = null;
        if (this.patternsCondition != null) {
            patterns = this.patternsCondition.getMatchingCondition(request);
            if (patterns == null) {
                return null;
            }
        }
        RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request);
        if (custom == null) {
            return null;
        }
        return new RequestMappingInfo(
                this.name, pathPatterns, patterns, methods, params, headers, consumes, produces, custom);
    }

4. Match类

AbstractHandlerMethodMapping类中的私有内部类,围绕匹配的HandlerMethod及其映射的瘦包装,用于在当前请求的上下文中将最佳匹配与比较器进行比较。

private class Match {

        private final T mapping;

        private final HandlerMethod handlerMethod;

        public Match(T mapping, HandlerMethod handlerMethod) {
            this.mapping = mapping;
            this.handlerMethod = handlerMethod;
        }

        @Override
        public String toString() {
            return this.mapping.toString();
        }
    }


标签:return,Nullable,Spring,request,private,RequestMappingInfo,mvc,null
From: https://blog.51cto.com/u_15668812/8786410

相关文章

  • 为什么Spring推荐使用构造器注入?
    在说注入时,我们先来回忆一下在Spring中依赖注入的方式1.setter注入2.构造器注入3.基于注解的注入 1.setter注入 优点:灵活性:可以动态地更改依赖对象,而不需要修改类的构造函数。可读性:可以清晰地看到类的依赖关系,便于理解和维护。测试性:便于进行单元测试,可以通过sette......
  • 【转载】Springboot2.x接收参数的多种方式
    参考https://blog.csdn.net/suki_rong/article/details/80445880https://zhuanlan.zhihu.com/p/34597391https://juejin.cn/post/6922469125033820168环境环境版本操作windows10JDK11Springboot2.3.12.RELEASE正文packagecom.example.demo.co......
  • Java-SpringBean的生命周期
    Java-SpringBean的生命周期简单版实例化(Instantiation):当Spring容器启动时,它会检查配置文件或注解,然后实例化所有在配置中声明的Bean。这是通过构造函数或工厂方法进行的。属性设置(PopulationofProperties):容器实例化Bean后,会通过依赖注入或者setter方法将配置的......
  • spring bean的生命周期
    springbean的生命周期分为六个阶段阶段一:容器启动阶段主要完成了扫描、实例化beanDefinitino对象、注册BeanPostProcessor、验证beanDefinition是否合格阶段二:Bean的实例化阶段主要推断实例化方式、实例化对象阶段三:bean的属性注入提前暴露、循环依赖做支持、查找注入信息......
  • springboot下添加日志模块和设置日志文件输出
    前言日志的使用将通过SLF4J来使用,SLF4J(SimpleLoggingFacadeforJava)是一个为Java应用提供简单日志记录的接口。它的主要目标是在不同的日志系统之间提供一个简单的抽象层,使得应用能够以一种灵活的方式切换日志实现,而不需要修改应用本身的代码。SLF4J不是一个具体的日志实现,而......
  • 【SpringBootWeb入门-9】分层解耦-分层解耦(IOC-DI引入)
    1、分层解耦概念上一节我们讲解了三层架构,我们把web程序分为了三层,分别是Conroller控制层、Service业务逻辑层、DAO数据访问层,这一节我们来讲解分层之后的解耦。解耦的含义就是接触耦合,首先我们来介绍两个概念:内聚、耦合。内聚:软件中各个功能模块内部的功能联系。耦合:衡量软......
  • Spring Mvc
    SpringMvcSpringMvc应用(常规使用)MVC架构MVC全名ModelViewController,是模型(model)一个试图(view)一个控制器(controller)的缩写.是一种用于创建Web应用程序表现层的模式Model(模型):包含业务模型和数据模型,数据模型用于封装数据,业务模型用于处理业务View(视图)通常指的就是......
  • SpringBoot+Vue实现大文件分块上传
    1.项目背景由于用户需求,需要上传大量图片,只能通过上传压缩包的形式上传,可是压缩包过大时,又会出现上传超时的情况,故需要将压缩包分块上传,然后解压缩图片、若图片过大则再对图片进行压缩。2.分块上传分块上传我在用的时候发现有两种:第一种:分块合并接口全由后端接口生成;第二种:前端......
  • .NET Core MVC基础之返回文件类型
    .NETCoreMVC基础之返回文件类型......
  • springboot下添加全局异常处理和自定义异常处理
    前言在spring项目中,优雅处理异常,好处是可以将系统产生的全部异常统一捕获处理,自定义的异常也由全局异常来捕获,如果涉及到validator参数校验器使用全局异常捕获也是较为方便。相关代码:GlobalExceptionHandler类:@Slf4j@RestControllerAdvicepublicclassGlobalExceptionHandl......