spring-web源码解析之ContentNegotiationManager

简介: 基于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对应的后缀名

目录
相关文章
|
7天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
8天前
yolo-world 源码解析(六)(2)
yolo-world 源码解析(六)
18 0
|
8天前
yolo-world 源码解析(六)(1)
yolo-world 源码解析(六)
12 0
|
8天前
yolo-world 源码解析(五)(4)
yolo-world 源码解析(五)
21 0
|
8天前
yolo-world 源码解析(五)(1)
yolo-world 源码解析(五)
31 0
|
8天前
yolo-world 源码解析(二)(2)
yolo-world 源码解析(二)
21 0
|
8天前
Marker 源码解析(二)(3)
Marker 源码解析(二)
14 0
|
23天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
26天前
|
存储 NoSQL 算法
【Redis技术进阶之路】「底层源码解析」揭秘高效存储模型与数据结构底层实现(字典)(二)
【Redis技术进阶之路】「底层源码解析」揭秘高效存储模型与数据结构底层实现(字典)
43 0

推荐镜像

更多