当前位置:网站首页>13. Content Negotiation
13. Content Negotiation
2022-08-10 23:49:00 【To learn in school Wolffy】
13.1 基本介绍
根据客户端接收能力不同,SpringBoot 返回不同媒体类型的数据
比如:
● 客户端 Http 请求
Accept: application/xml
则返回 xml 数据
● 客户端 Http 请求Accept: application/json
则返回 json 数据For example the diagram below
13.2 内容协商-应用实例
- 需求说明:
- 使用Postman发送Http请求
- Depends on the request headers,返回对应的 json数据 或者 xml 数据
- 如下图所示
- 在 pom.xml 增加处理 xml 的依赖
<!--引入处理xml的依赖-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
使用Postmanemit different Http Header ,You can see the corresponding data format returned (特别说明:Please restart the project before testing)
切换 Postman 不同的 Accept 类型,来 Debug 源码,看看对应的 JsonGenerator 类型
----- Accept 切换成 application/xml 进行Degug -----
- 使用浏览器请求,为什么会返回 xml 数据,而不是 json ?
13.3 注意事项和使用细节
Postman 可以通过修改 Accept 的值,to return different data formats
对于浏览器,We cannot modify it Accept 的值,怎么办?
=> 解决方案:Enable support for content negotiation based on request parameters
- 修改 application.yml,开启基于请求参数的内容协商功能
spring:
mvc:
# static-path-pattern: /xjsres/** #修改静态资源访问的路径/前缀
hiddenmethod:
filter:
enabled: true #启用了 HiddenHttpMethodFilter 支持 rest风格请求
view: # 配置了视图解析器
suffix: .html
prefix: / # 这里需要注意 前缀prefix 要考虑 static-path-pattern 的配置
contentnegotiation:
favor-parameter: true # 开启基于请求参数的内容协商功能
web:
resources:
#修改/指定 静态资源的 存放路径/位置 # String[] staticLocations
#注意:配置了 static-locations 原来的访问路径就被覆盖了,如果需要保留,要再指定一下
static-locations: ["classpath:/xjsimg/", "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"]
- 完成测试
- 注意,参数
format
是规定好的 , After enabling the content negotiation function of request parameters,SpringBoot 底层ParameterContentNegotiationStrategy
会通过 format 来接收参数,然后返回对应的 媒体类型/数据格式,当然format=xx
这个 xx 媒体类型/数据格式 是 SpringBoot It can be dealt with,不能乱写
- 也可以自定义配置 The request parameter name for content negotiation
spring:
mvc:
# static-path-pattern: /xjsres/** #修改静态资源访问的路径/前缀
hiddenmethod:
filter:
enabled: true #启用了 HiddenHttpMethodFilter 支持 rest风格请求
view: # 配置了视图解析器
suffix: .html
prefix: / # 这里需要注意 前缀prefix 要考虑 static-path-pattern 的配置
contentnegotiation:
favor-parameter: true # 开启基于请求参数的内容协商功能
parameter-name: xjsformat # 我们自定义一个 Parameter name for content negotiation [默认是format]
web:
resources:
#修改/指定 静态资源的 存放路径/位置 # String[] staticLocations
#注意:配置了 static-locations 原来的访问路径就被覆盖了,如果需要保留,要再指定一下
static-locations: ["classpath:/xjsimg/", "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"]
边栏推荐
猜你喜欢
Unity--URP渲染管线实战教程系列之URP摄像机核心机制剖析
关于弱监督学习的详细介绍——A Brief Introduction to Weakly Supervised Learning
How to recover deleted files from the recycle bin, two methods of recovering files from the recycle bin
浅析工业互联网
Kioptrix Level 1 靶机wp
服务器小常识
《剑指offer》题解——week2(持续更新)
promise详解
【C语言】二分查找(折半查找)
ROS Experimental Notes - Install QPEP and Intel-MKL
随机推荐
CSDN21天学习挑战赛之折半插入排序
HGAME 2022 Week4 writeup
Kubernetes 选举机制HA
excel英文自动翻译成中文教程
大厂面试热点:“热修复机制及常见的几个框架介绍”
proxy代理服务_2
Timers, synchronous and asynchronous APIs, file system modules, file streams
好用的翻译插件-一键自动翻译插件软件
HCTF 2018 WarmUP writeup
mysql数据库高级操作
Excel English automatic translation into Chinese tutorial
VMware 虚拟机开启Ip地址自动更换解决
CDN原理与应用简要介绍
推进牛仔服装的高质量发展
[C language] First understanding of pointers
安科瑞为工业能效行动计划提供EMS解决方案-Susie 周
电脑桌面删除的文件回收站没有,电脑上桌面删除文件在回收站找不到怎么办
Multilingual Translation - Multilingual Translation Software Free
String
烘干衣服问题