spring-web源码解析之ContentNegotiationManager

本文涉及的产品
云解析 DNS,旗舰版 1个月
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
简介: 基于4.1.7.RELEASE此类实现了两个接口,一个是 ContentNegotiationStrategy ,一个是 MediaTypeFileExtensionResolver ,由此它就可以完成两种功能1 根据request中的内容,解析出MediaType的List列表。

基于4.1.7.RELEASE

此类实现了两个接口,一个是 ContentNegotiationStrategy ,一个是 MediaTypeFileExtensionResolver ,由此它就可以完成两种功能

1 根据request中的内容,解析出MediaType的List列表。

2 根据MediaType,解析出对应的url后缀名。

该类只有含ContentNegotiationStrategy参数和无参数的构造函数,在前者,其会解析参数是否也实现了MediaTypeFileExtensionResolver接口,这个接口前面讲过,是负责将MediaType解析出url后缀的接口类,如果实现了,那么会将其注册到Set类型私有变量fileExtensionResolvers中去。

public ContentNegotiationManager(ContentNegotiationStrategy... strategies) {
   Assert.notEmpty(strategies, "At least one ContentNegotiationStrategy is expected");
   this.contentNegotiationStrategies.addAll(Arrays.asList(strategies));
   for (ContentNegotiationStrategy strategy : this.contentNegotiationStrategies) {
      if (strategy instanceof MediaTypeFileExtensionResolver) {
         this.fileExtensionResolvers.add((MediaTypeFileExtensionResolver) strategy);
      }
   }
}

另一种形式为

public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies) {
   Assert.notEmpty(strategies, "At least one ContentNegotiationStrategy is expected");
   this.contentNegotiationStrategies.addAll(strategies);
   for (ContentNegotiationStrategy strategy : this.contentNegotiationStrategies) {
      if (strategy instanceof MediaTypeFileExtensionResolver) {
         this.fileExtensionResolvers.add((MediaTypeFileExtensionResolver) strategy);
      }
   }
}

功能都是一样,只是参数不同而已。

另一种构造函数是无参数的构造函数,这里会注册一个HeaderContentNegoaiationStrategy类到contentNegotiationStrategies列表,这个类之前也讲过是根据accept来解析MediaType的。

那么注册出来的列表怎么用呢?看如下方法。

public List<String> resolveFileExtensions(MediaType mediaType) {
   Set<String> result = new LinkedHashSet<String>();
   for (MediaTypeFileExtensionResolver resolver : this.fileExtensionResolvers) {
      result.addAll(resolver.resolveFileExtensions(mediaType));
   }
   return new ArrayList<String>(result);
}

这里让所有MediaTypeFileExtensionResolver去解析mediaType,然后将解析出来的后缀名集中返回。

或者另一种形式

public List<String> getAllFileExtensions() {
   Set<String> result = new LinkedHashSet<String>();
   for (MediaTypeFileExtensionResolver resolver : this.fileExtensionResolvers) {
      result.addAll(resolver.getAllFileExtensions());
   }
   return new ArrayList<String>(result);
}

直接获取所有的后缀。

总结:该类通过内置的内容协商策略和扩展名解析器,将request中的mediaType解析出来,并找到对应的后缀名,可以认为本来有2个作用

1 通过request解析出对应的mediaType

2 获取全部或者mediaType对应的后缀名

目录
相关文章
|
15天前
|
存储 NoSQL Redis
redis 6源码解析之 object
redis 6源码解析之 object
43 6
|
4天前
|
XML Java 数据格式
Spring Cloud全解析:注册中心之zookeeper注册中心
使用ZooKeeper作为Spring Cloud的注册中心无需单独部署服务器,直接利用ZooKeeper服务端功能。项目通过`spring-cloud-starter-zookeeper-discovery`依赖实现服务注册与发现。配置文件指定连接地址,如`localhost:2181`。启动应用后,服务自动注册到ZooKeeper的`/services`路径下,形成临时节点,包含服务实例信息。
|
8天前
|
开发者 Python
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
32 1
|
8天前
|
开发者 Python
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
21 1
|
22天前
|
负载均衡 Java Spring
@EnableFeignClients注解源码解析
@EnableFeignClients注解源码解析
48 14
|
15天前
|
NoSQL Redis
redis 6源码解析之 ziplist
redis 6源码解析之 ziplist
16 5
|
3天前
|
算法 安全 Java
深入解析Java多线程:源码级别的分析与实践
深入解析Java多线程:源码级别的分析与实践
|
16天前
|
XML Java 数据库连接
深入解析 Spring 配置文件:从基础到高级
【8月更文挑战第3天】Spring配置文件是构建与管理Spring应用的核心,它涵盖了从基础到高级的各种配置技巧。基础配置采用`.xml`格式定义Bean及其依赖;中级配置包括设置Bean作用域及引入属性文件;高级配置则涉及AOP、事务管理和与其他框架的整合。熟练掌握这些配置能帮助开发者构建出更为灵活且易维护的应用系统。
|
2月前
|
XML Java 数据格式
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
67 3
|
22天前
|
负载均衡 Java API
Feign 进行rpc 调用时使用ribbon负载均衡源码解析
Feign 进行rpc 调用时使用ribbon负载均衡源码解析
38 11

推荐镜像

更多