内容协商原理-HttpMessageConverter

  1. 内容协商原理是什么
    1. 内容协商 (Content Negotiation) 是指客户端和服务器之间通过协商确定交换的内容格式,以使得双方能够适配并理解对方的数据。在 HTTP 协议中,内容协商是通过请求头中的 "Accept" 字段和响应头中的 "Content-Type" 字段来进行的
    2. HttpMessageConverter 是 Spring MVC 框架提供的一个接口用于将 HTTP 请求和响应的消息体与 Java 对象之间进行相互转换。它负责处理请求的参数,并将处理结果转换成指定的响应类型,并将其写入 HTTP 响应中。
    3. Spring MVC 中的 HttpMessageConverter 是内容协商的重要组成部分,它根据客户端请求中的 MIME 类型(通过 "Accept" 字段指定)和服务器端提供的 MIME 类型(通过 "Content-Type" 字段指定),选择相应的 HttpMessageConverter 来完成请求和响应的转换工作。
    4. 在响应阶段,Spring MVC 会根据控制器方法返回值的类型以及响应头中的 "Accept" 字段来确定客户端期望接收的响应内容类型。再根据这个 MIME 类型来选择合适的 HttpMessageConverter,将处理结果转换为对应的响应报文返回给客户端。
    5. HttpMessageConverter 是 Spring MVC 框架中负责处理 HTTP 请求和响应的消息体转换的接口。
  2. HttpMessageConverter 怎么工作?何时工作?(RequestMappingHandlerAdapter是适配器)
    1. 如果controller方法的返回值标注了 @ResponseBody 注解------@ResponseBody注解告诉Spring MVC不要对方法返回值进行视图解析,而是使用合适的HttpMessageConverter将返回值转换为特定的数据格式,并将其写入HTTP响应体中,从而实现直接返回数据给客户端的功能

    2. HttpMessageConverter先进行内容协商
      1. 遍历所有的MessageConverter看谁支持这种内容类型的数据
      2. 默认MessageConverter有以下
      3. 最终因为要json所以MappingJackson2HttpMessageConverter支持写
      4. jsonjackson用ObjectMapper把对象写出去
  3. 定制 HttpMessageConverter 来实现多端内容协商
    1. 引入支持yaml的包
    2. 编写配置,新增一种媒体类型
      spring.mvc.contentnegotiation.media-types.yaml=text/yaml
    3. 增加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组件

    4. 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);
              }
      
          }
      }

      这段代码好好分析

  4. 编写WebMvcConfigurer提供的configureMessageConverters底层,修改底层的MessageConverter
    1. 为什么要在WebMvcConfigurer提供的configureMessageConverters底层修改来定制HttpMessageConverter来实现多端内容协商?????====因为WebMvcConfigurer这个文件是什么?(提供了配置SpringMVC底层的所有组件入口
    2. configureMessageConverters------在Spring MVC中,可以通过重写configureMessageConverters方法来自定义和配置HttpMessageConverter。

======================================================

  1. @ResponseBody由HttpMessageConverter处理,什么意思,又是怎么进行处理的
    1. @ResponseBody注解用于将方法返回的对象直接写入HTTP响应体中,通常用于返回JSON、XML等格式的数据。@ResponseBody注解告诉Spring MVC框架不要对方法的返回值进行视图解析,而是使用HttpMessageConverter来处理返回值并将其写入响应体。

    2. 当一个方法被标记为@ResponseBody时,Spring MVC会根据请求头中的"Accept"字段和方法的返回类型,选择合适的HttpMessageConverter来处理方法返回的对象。HttpMessageConverter负责将Java对象转换为特定的数据格式,例如JSON、XML等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值