当前位置:网站首页>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/"]
边栏推荐
猜你喜欢
逮到一个阿里 10 年老 测试开发,聊过之后收益良多...
回收站的文件删了怎么恢复,回收站文件恢复的两种方法
Parse method's parameter list (including parameter names)
14. Thymeleaf
iNFTnews | Web3时代,用户将拥有数据自主权
[C] the C language program design, dynamic address book (order)
C语言篇,操作符之 移位运算符(>>、<<)详解
Deep Learning Transformer Architecture Analysis
CW617N锡青铜 CuZn40Pb2对应牌号
矩阵的迹(详解)
随机推荐
VR全景+安全科普教育,让学生们提高安全意识
正交基(线性代数)
web 性能提升(将持续更新……)
HGAME 2022 Week4 writeup
HGAME 2022 Week2 writeup
2022下半年软考「高项」易混淆知识点汇总(2)
sklearn.datasets.make_circles
There is no recycle bin for deleted files on the computer desktop, what should I do if the deleted files on the desktop cannot be found in the recycle bin?
2022牛客多校(七)K. Great Party博弈方法证明
Geogebra 教程之 03 没有铅笔的数学
Kubernetes 维护技术分享
一文带你了解 ViewModel 的使用及源码解析
Anroid 组件化构架设计:细说为何需要使用组件化提高工程编译速度
一条SQL查询语句是如何执行的?
【C语言篇】操作符之 位运算符详解(“ << ”,“ >> ”,“ & ”,“ | ”,“ ^ ”,“ ~ ”)
ROS实验笔记之——UZH-FPV数据集的验证
三栏布局实现
花环灯问题
宝塔实测-搭建PHP在线模拟考试系统
Activiti7子流程之Call activity