内容协商原理是什么
- 内容协商 (Content Negotiation) 是指客户端和服务器之间通过协商确定交换的内容格式,以使得双方能够适配并理解对方的数据。在 HTTP 协议中,内容协商是通过请求头中的 "Accept" 字段和响应头中的 "Content-Type" 字段来进行的
- HttpMessageConverter 是 Spring MVC 框架提供的一个接口,用于将 HTTP 请求和响应的消息体与 Java 对象之间进行相互转换。它负责处理请求的参数,并将处理结果转换成指定的响应类型,并将其写入 HTTP 响应中。
- Spring MVC 中的 HttpMessageConverter 是内容协商的重要组成部分,它根据客户端请求中的 MIME 类型(通过 "Accept" 字段指定)和服务器端提供的 MIME 类型(通过 "Content-Type" 字段指定),选择相应的 HttpMessageConverter 来完成请求和响应的转换工作。
- 在响应阶段,Spring MVC 会根据控制器方法返回值的类型以及响应头中的 "Accept" 字段来确定客户端期望接收的响应内容类型。再根据这个 MIME 类型来选择合适的 HttpMessageConverter,将处理结果转换为对应的响应报文返回给客户端。
- HttpMessageConverter 是 Spring MVC 框架中负责处理 HTTP 请求和响应的消息体转换的接口。
HttpMessageConverter
怎么工作?何时工作?(RequestMappingHandlerAdapter是适配器)-
如果controller方法的返回值标注了
@ResponseBody
注解------@ResponseBody
注解告诉Spring MVC不要对方法返回值进行视图解析,而是使用合适的HttpMessageConverter
将返回值转换为特定的数据格式,并将其写入HTTP响应体中,从而实现直接返回数据给客户端的功能 HttpMessageConverter
会先进行内容协商- 遍历所有的
MessageConverter
看谁支持这种内容类型的数据 - 默认
MessageConverter
有以下 - 最终因为要
json
所以MappingJackson2HttpMessageConverter
支持写 - jsonjackson用
ObjectMapper
把对象写出去
- 遍历所有的
-
- 定制
HttpMessageConverter
来实现多端内容协商- 引入支持yaml的包
- 编写配置,新增一种媒体类型
spring.mvc.contentnegotiation.media-types.yaml=text/yaml
-
增加
HttpMessageConverter
组件,专门负责把对象写出为yaml格式。(@Configuration //这是一个配置类,给容器中放一个 WebMvcConfigurer 组件,就能自定义底层)@Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override //配置一个能把对象转为yaml的messageConverter public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MyYamlHttpMessageConverter()); } }; }
注意MyYamlHttpMessageConverter()是自己要写的
HttpMessageConverter
组件 -
HttpMessageConverter的示例写法
public class MyYamlHttpMessageConverter extends AbstractHttpMessageConverter<Object> { private ObjectMapper objectMapper = null; //把对象转成yaml public MyYamlHttpMessageConverter(){ //告诉SpringBoot这个MessageConverter支持哪种媒体类型 //媒体类型 super(new MediaType("text", "yaml", Charset.forName("UTF-8"))); YAMLFactory factory = new YAMLFactory() .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER); this.objectMapper = new ObjectMapper(factory); } @Override protected boolean supports(Class<?> clazz) { //只要是对象类型,不是基本类型 return true; } @Override //@RequestBody protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { return null; } @Override //@ResponseBody 把对象怎么写出去 protected void writeInternal(Object methodReturnValue, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { //try-with写法,自动关流 try(OutputStream os = outputMessage.getBody()){ this.objectMapper.writeValue(os,methodReturnValue); } } }
这段代码好好分析
- 编写
WebMvcConfigurer
提供的configureMessageConverters
底层,修改底层的MessageConverter
为什么要在WebMvcConfigurer
提供的configureMessageConverters
底层修改来定制HttpMessageConverter
来实现多端内容协商?????====因为WebMvcConfigurer这个文件是什么?(
提供了配置SpringMVC底层的所有组件入口)
configureMessageConverters------
在Spring MVC中,可以通过重写configureMessageConverters
方法来自定义和配置HttpMessageConverter。
======================================================
- @ResponseBody由HttpMessageConverter处理,什么意思,又是怎么进行处理的
-
@ResponseBody
注解用于将方法返回的对象直接写入HTTP响应体中,通常用于返回JSON、XML等格式的数据。@ResponseBody
注解告诉Spring MVC框架不要对方法的返回值进行视图解析,而是使用HttpMessageConverter来处理返回值并将其写入响应体。 -
当一个方法被标记为
@ResponseBody
时,Spring MVC会根据请求头中的"Accept"字段和方法的返回类型,选择合适的HttpMessageConverter
来处理方法返回的对象。HttpMessageConverter
负责将Java对象转换为特定的数据格式,例如JSON、XML等。
-