内容协商

在 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. Flink mysql-cdc同步主键分布不均匀的mysql表

    一.背景 1.遇到问题描述 通过Flink同步mysql到iceberg中,任务一直在运行中,但是在目标表看不到数据.经排查发现job manager一直在做切片工作,切了一小时还没开始同步数据,日志 ...

  2. BurpSuite重放发包的一些区别

    目录 Send Group in parallel Single connection 和 Separate connections 的差别 实际应用场景 Reference 2022年之后,Burp ...

  3. C# DataTable 类使用

    命名空间: System.Data 程序集: System.Data.Common.dll 参考连接:https://docs.microsoft.com/zh-cn/dotnet/api/syste ...

  4. 离线安装Docker社区版详细教程

    Docker是一个开源的应用容器引擎,使得开发者可以打包应用以及依赖包到一个轻量级的.可移植的容器中,然后发布到任何支持Docker的平台上.本文将详细介绍如何在离线环境下安装Docker社区版. 1 ...

  5. MySQL - [07] 查看库表数据所使用的空间大小

    1.切换数据库:use information_schema; 2.查看数据库使用大小 SELECT concat(round(sum(data_length/1024/1024),2),'MB') ...

  6. 阿里云Windows server 2016服务器Antimalware Service Executable进程占比高,cpu接近100%,强制关闭该进程实测

    问题描述:阿里云Windows server 2016服务器Antimalware Service Executable进程占比高,cpu接近100%,需要强制关闭该进程,排查问题,进入系统服务关闭, ...

  7. 云服务器Linux 时间与本地时间不一致

      云服务器Linux 时间与本地时间不一致 问题解释: 云服务器和本地计算机之间的时间不一致可能是因为它们使用的时间同步服务不同,或者云服务器没有配置自动对时. 解决方法: 手动同步时间:可以使用d ...

  8. PPT-产品页图片并茂

    一.好的文案原则 图片并茂 -重点突出 二.操作 抠图 拷贝图片到ppt页面->选中图片->双击删除背景->标记要删除的区域 背景替换 1.复制图片->粘贴->置于底层 ...

  9. 通过 C# 打印Word文档

    Word文档是日常办公和学习中不可或缺的一部分.比如在商务往来中,经常需要打印 Word 文档用于撰写和传递正式的商务信函.合作协议.项目提案等.打印出来的文档便于双方签字盖章,具有法律效力和正式性. ...

  10. Laravel11 从0开发 Swoole-Reverb 扩展包(二) - Pusher 协议介绍

    Pusher 协议概述 Pusher 协议 是一种用于实时 Web 通信的协议,它基于 WebSocket 技术,并提供了一套 发布-订阅(Pub/Sub)模式,用于让客户端(如浏览器.移动端.后端服 ...