0
点赞
收藏
分享

微信扫一扫

内容协商

  • 根据客户端接收能力不同,返回不同媒体类型的数据

# 前提是引入xml依赖:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>

  • postman分别测试返回json和xml:只需要改变请求头中Accept字段。Http协议中规定的,告诉服务器本客户端可以接收的数据类型
  • 内容协商_客户端

  • 浏览器中的内容协商
  • 内容协商_xml_02

由于浏览器中无法像postman那样设置响应格式
可在yml中开启请求参数内容协商模式:

spring:
contentnegotiation:
favor-parameter: true

# 开启后前端发送请求
http://localhost:8080/test/person?format=json # 响应给客户端json
http://localhost:8080/test/person?format=xml # 响应给客户端xml



举报

相关推荐

0 条评论