内容协商

在 HTTP 协议中,内容协商是一种机制,用于为同一 URI 提供资源不同的表示形式,以帮助用户代理指定最适合用户的表示形式(例如,哪种文档语言、哪种图片格式或者哪种内容编码)。[^1]

SpringBoot内容协商

  1. 基于请求头内容协商(默认开启)
  • 客户端向服务端发送请求,携带HTTP标准的Accept请求头。Accept: application/jsontext/xml、`text/yaml
  • 服务端根据客户端请求头期望的数据类型进行动态返回
  1. 基于请求参数内容协商(需要开启)
  • 客户端发送请求 GET /projects/spring-boot?format=json
  • 服务端匹配到@GetMapping("/projects/spring-boot") ,根据参数协商,优先返回 json 类型数据【需要开启参数匹配设置】

开启参考内容协商设置

spring:
mvc:
contentnegotiation: # 内容协商
favor-parameter: true # 是否应该使用请求参数(默认为“format”)来确定请求的媒体类型。默认为false
parameter-name: format # 启用“favor-parameter”时使用的参数名称。不设置时使用“format”

代码示例

pom.xml

需要测试返回xml格式的数据,这里额外导入了jackson-dataformat-xml配置

点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu</groupId>
<artifactId>boot3-04-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot3-04-web</name>
<description>boot3-04-web</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build> </project>

bean

因为测试需要xml的返回结果,这里配置了@JacksonXmlRootElement,如果不需要可以不配置,json的返回结果SpringBoot默认是支持的。

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JacksonXmlRootElement
public class Person {
private Long id;
private String name;
private String email;
private Integer age;
}

controller

@Slf4j
@RestController
public class HelloController {
@GetMapping("/person")
public Person person(){
return Person.builder()
.id(1L)
.name("张三")
.email("110@gamil.com")
.age(18)
.build();
}
}

application.yml

spring:
mvc:
contentnegotiation: # 内容协商
favor-parameter: true # 是否应该使用请求参数(默认为“format”)来确定请求的媒体类型。默认为false
parameter-name: format # 启用“favor-parameter”时使用的参数名称。不设置时使用“format”

客户端请求

  1. 基于请求头内容协商



  2. 基于请求参数内容协商



参考:

[1] https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Content_negotiation

[2] https://www.yuque.com/leifengyang/springboot3/wp5l9qbu1k64frz1#haGAM

SpringBoot内容协商(Content Negotiation)的更多相关文章

  1. Spring MVC Content Negotiation 转载

    Spring MVC Content Negotiation 2017年11月15日 00:21:21 carl-zhao 阅读数:2983   Spring MVC有两种方式生成output的方法: ...

  2. Content Negotiation(内容协商)

    Asp.Net Web API 2第十四课——Content Negotiation(内容协商)   前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http:// ...

  3. Content Negotiation in ASP.NET Web API

    本文描述Web API实现内容协商(content negotiation). The HTTP specification (RFC 2616) defines content negotiatio ...

  4. WebApi2官网学习记录---Content Negotiation

    Content Negotiation的意思是:当有多种Content-Type可供选择时,选择最合适的一种进行序列化并返回给client. 主要依据请求中的Accept.Accept-Charset ...

  5. Content Negotiation using Spring MVC

    There are two ways to generate output using Spring MVC: You can use the RESTful @ResponseBody approa ...

  6. Asp.Net Web API 2第十四课——Content Negotiation(内容协商)

    前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文描述ASP.NET W ...

  7. 每一个web开发者都应该了解的HTTP/2

    我认为每一个 web 开发者都应该对这个支撑了整个 Web 世界的 HTTP 协议有所了解,这样才能帮助你更好的完成开发任务.在这篇文章中,我将讨论什么是 HTTP,它是怎么产生的,它的地位,以及我们 ...

  8. HTTP/1.1协议(中文归纳版)

    一.介绍(introduction) 1. 目的——HTTP/0.9-〉HTTP/1.0-〉HTTP/1.1 2. 要求——MUST.REQUIRED.SHOULD 3. 术语——连接(Connect ...

  9. 从头编写 asp.net core 2.0 web api 基础框架 (1)

    工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相 ...

  10. 【转载】从头编写 asp.net core 2.0 web api 基础框架 (1)

    工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相 ...

随机推荐

  1. Q:oracle中blog中截取部分字符串

    blog报文中获取对应标签字符串 将xxx替换成需要查询的标签 to_char(substr(C_INPUT,instr(C_INPUT,'<xxx>')+length('<xxx& ...

  2. centos系统chrony时间同步

    centos系统chrony时间同步 概要 chrony 是网络时间协议(Network Time Protocol )的通用实现.它不但可以提供保持系统时间与 NTP 时钟服务器同步的服务,还能作为 ...

  3. git pull报错:Pulling without specifying how to reconcile divergent branches is discouraged.

    一.保存内容如下 二.翻译 三.设置为默认即可:git config pull.rebase false

  4. SpringBoot整合富文本编辑器(UEditor)

    UEditro是一款比较好用的富文本编辑器,所谓的富文本编辑器就是和服务器交互的数据不是普通的字符串文件,而是一些内容包含比较广的字符串,一般是指的html页面,目前比较好用的是百度的UEditor, ...

  5. ADF - [01] 概述

    大数据需要可以启用协调和操作过程以将这些巨大的原始数据存储优化为可操作的业务见解的服务. Azure 数据工厂是专为复杂的混合提取-转换-加载 (ETL).提取-加载-转换 (ELT) 和数据集成项目 ...

  6. Kettle - 使用案例

    原文链接:https://blog.csdn.net/gdkyxy2013/article/details/117106691 案例一:把seaking的数据按id同步到seaking2,seakin ...

  7. MySQL - [01] 安装部署

    题记部分 一.Windows安装部署 1.1.下载 (1)官网下载 MySQL的安装包,点此访问官网.[注意:MySQL不要安装到系统盘] (2)点击 DOWNLOADS > MySQL Com ...

  8. python 两个函数间如何调用

    def a(): pass def b(): pass s=a() b(s) 或者 b(a())

  9. leetcode两数之和变种(找出所有满足总和的两个数)

    偶尔看到leetcode 的两数之和,但是之前遇到过两数之和的变种,之前一开始想不出来,后面看了别人的题解才想到解法,这里记录一下. 题目描述: 原leetcode题目描述 给定一个整数数组 nums ...

  10. npm 如何更新项目最新依赖包

    NPM 是什么? Node 软件包管理器(NPM)提供了各种功能来帮助你安装和维护项目的依赖关系. 由于错误修复.新功能和其他更新,依赖关系可能会随着时间的推移而变得过时.你的项目依赖越多,就越难跟上 ...