当前位置:网站首页>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/"]
边栏推荐
猜你喜欢
随机推荐
虎符CTF 2022 Quest-Crash Writeup
22年全国程序员1月薪资出炉,才知道年薪 40 万以上的有这么多?
SQL注入基础
细谈APP开发焦点问题:AMS 系统时间调节原理
Metasploit——客户端渗透
Talking about jsfuck coding
Parse method's parameter list (including parameter names)
3. 容器功能
性能不够,机器来凑;jvm调优实战操作详解
[C language] Detailed explanation of data storage
【C语言】数据储存详解
翻译软件哪个准确度高【免费】
部分准备金银行已经过时
DASCTF 2022 7月赋能赛 writeup
高校就业管理系统设计与实现
CSAPP lab0 实验环境搭建
HGAME 2022 复盘 writeup
大厂面试热点:“热修复机制及常见的几个框架介绍”
App基础优化三部曲:启动原理&黑白屏优化&启动时间优化
HCTF 2018 WarmUP writeup