SpringBoot---web-->>自定义MessageConverter

自定义MessageConverter

之前的学习中,方法加一个@ResponseBody注解就能把返回的对象转换为json或xml格式响应在页面上,通过获取请求头信息的方式或者开启参数内容协商功能实现获取指定类型的数据。但是能用的都是规定好的,假如现在需要把浏览器发送的请求返回的数据返回为xml格式,参数请求返回的数据为json,app返回的数据为aaaa自定类型,就要自己定义一个MessageConverter

自定义Converter,就是定制化配置SpringBoot底层,刚学springBoot就知道定制化配置可以在配置类里面加返回类型为 WebMvcConfigurer 这个接口的组件方法。然后返回一个 WebMvcConfigurer 对象,这个接口里面有很多添加定制化组件的方法。

进入这个接口,找到两个方法

/**
 * Configure the {@link HttpMessageConverter HttpMessageConverter}s for
 * reading from the request body and for writing to the response body.
 * <p>By default, all built-in converters are configured as long as the
 * corresponding 3rd party libraries such Jackson JSON, JAXB2, and others
 * are present on the classpath.
 * <p><strong>Note</strong> use of this method turns off default converter
 * registration. Alternatively, use
 * {@link #extendMessageConverters(java.util.List)} to modify that default
 * list of converters.
 * @param converters initially an empty list of converters
 */
//上面这一堆注释意思就是用了这个方法默认启动时的那些报文转换器就会被覆盖,用不成了,就是完全自己定制化
default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
}
​
/**
 * Extend or modify the list of converters after it has been, either
 * {@link #configureMessageConverters(List) configured} or initialized with
 * a default list.
 * <p>Note that the order of converter registration is important. Especially
 * in cases where clients accept {@link org.springframework.http.MediaType#ALL}
 * the convert
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值