SpringBoot内容协商(Content Negotiation)
内容协商
在 HTTP 协议中,内容协商是一种机制,用于为同一 URI 提供资源不同的表示形式,以帮助用户代理指定最适合用户的表示形式(例如,哪种文档语言、哪种图片格式或者哪种内容编码)。[^1]
SpringBoot内容协商
- 基于请求头内容协商(默认开启)
- 客户端向服务端发送请求,携带HTTP标准的Accept请求头。
Accept:application/json、text/xml、`text/yaml- 服务端根据客户端请求头期望的数据类型进行动态返回
- 基于请求参数内容协商(需要开启)
- 客户端发送请求
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] https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Content_negotiation
[2] https://www.yuque.com/leifengyang/springboot3/wp5l9qbu1k64frz1#haGAM
SpringBoot内容协商(Content Negotiation)的更多相关文章
- Spring MVC Content Negotiation 转载
Spring MVC Content Negotiation 2017年11月15日 00:21:21 carl-zhao 阅读数:2983 Spring MVC有两种方式生成output的方法: ...
- Content Negotiation(内容协商)
Asp.Net Web API 2第十四课——Content Negotiation(内容协商) 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http:// ...
- Content Negotiation in ASP.NET Web API
本文描述Web API实现内容协商(content negotiation). The HTTP specification (RFC 2616) defines content negotiatio ...
- WebApi2官网学习记录---Content Negotiation
Content Negotiation的意思是:当有多种Content-Type可供选择时,选择最合适的一种进行序列化并返回给client. 主要依据请求中的Accept.Accept-Charset ...
- Content Negotiation using Spring MVC
There are two ways to generate output using Spring MVC: You can use the RESTful @ResponseBody approa ...
- Asp.Net Web API 2第十四课——Content Negotiation(内容协商)
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文描述ASP.NET W ...
- 每一个web开发者都应该了解的HTTP/2
我认为每一个 web 开发者都应该对这个支撑了整个 Web 世界的 HTTP 协议有所了解,这样才能帮助你更好的完成开发任务.在这篇文章中,我将讨论什么是 HTTP,它是怎么产生的,它的地位,以及我们 ...
- HTTP/1.1协议(中文归纳版)
一.介绍(introduction) 1. 目的——HTTP/0.9-〉HTTP/1.0-〉HTTP/1.1 2. 要求——MUST.REQUIRED.SHOULD 3. 术语——连接(Connect ...
- 从头编写 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的相 ...
- 【转载】从头编写 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的相 ...
随机推荐
- 用virtual-manager安装虚拟机
使用virt-manager基本使用 启用机管理主窗口 硬件细节窗口 配置虚拟机启动选项 附加USB设备给虚拟机准备工作 USB重定向 虚拟机图形控制台 添加远程连接 显示虚拟机细节 性能监视 实验准 ...
- CUDA异常捕获
技术背景 在CUDA编程中有可能会遇到一些相对比较隐蔽的报错,但是直接编译运行cu文件是不显现的.那么可以通过添加一个用于检查的宏,来监测CUDA程序运行过程中可能出现的报错. error.cuh 我 ...
- Kubernetes - [02] 网络通讯方式
题记部分 一.网络通讯模式 Kubernetes的网络模型假定了所有Pod都在一个可以直接连通的扁平的网络空间中,这在(GCEGoogle Compute Engine)里面是现成的网络模型,Ku ...
- UE5 C++ 程序进程退出
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include ...
- 【记录】C-文件输入输出
写在开头 摸鱼摸得昏天黑地,是该炸一炸鱼的本愿了~ 以前总觉得笔记没什么重要的,但有时事到临头,又十分渴求简明的提纲来唤起记忆/提供重学的门路.于是就有了以下的产物,也希望能抑制一下无效摸鱼的堕落 ...
- Netty基础—2.网络编程基础二
大纲 1.网络编程简介 2.BIO网络编程 3.AIO网络编程 4.NIO网络编程之Buffer 5.NIO网络编程之实战 6.NIO网络编程之Reactor模式 1.网络编程简介 既然是通信,那么肯 ...
- AI 大模型:现状、挑战与未来多维度发展趋势
在科技浪潮的推动下,以 Deepseek 为代表的 AI 大模型正以颠覆性力量重塑产业格局.从金融风控到工业质检,从智慧医疗到智能教育,这些轻量化的 AI 工具不仅打破了传统工作模式的桎梏,更构建起一 ...
- 实现Windows之间(win10)的桌面连接的三步走方法
实现Windows之间(win10)的远程桌面连接的三步走方法 目录 目录 实现Windows之间(win10)的远程桌面连接的三步走方法 目录 环境 step1:打开两台Windows电脑的 ...
- dockerfile 由于公钥不可用,无法验证以下签名
报错 当我在打包 docker镜像时,发生了报错 $ sudo docker build -t dcgm-exporter:3.2.5 . 1.772 The following signatures ...
- NumPy学习5
今天学习了11, NumPy数组元素增删改查NumPy 数组元素的增删改查操作,主要有以下方法:数组元素操作方法函数名称 描述说明resize 返回指定形状的新数组.append 将元素值添加到数组的 ...