HttpMessageConverter如果不指定Accept和Content-Type,那么HttpMessageConverter按顺序调用canRead方法判断(默认canRead方法中分别调用supports和重载的canRead方法,其中supports方法由子类实现,canRead方法根据MediaType判断,如果为null则返回true。但Jackson转换器重写了canRead方法)

自定义HttpMessageConverter

  • 实现请求参数为properties数据结构,返回json数据结构

1、创建自定义HttpMessageConverter

package com.cnbi.sourceread.mvc.converter;import com.cnbi.sourceread.bean.Person;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Properties;/*** @ClassName MappingPropertiesHttpMessageConverter* @Description Properties参数转换* @Author Wangjunkai* @Date 2021/2/20 23:28**/public class MappingPropertiesHttpMessageConverter extends AbstractHttpMessageConverter {public MappingPropertiesHttpMessageConverter(){super(Charset.forName("UTF-8"), MediaType.valueOf("application/properties"));}@Overrideprotected boolean supports(Class clazz) {return clazz.isAssignableFrom(Person.class);}@Overrideprotected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {InputStream body = inputMessage.getBody();Properties properties = new Properties();properties.load(body);return new Person(Long.parseLong(properties.getProperty("person.age")), properties.getProperty("person.name"));}@Overrideprotected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {Properties properties = new Properties();if(o instanceof Person){Person person = (Person) o;properties.setProperty("person.age", String.valueOf(person.getAge()));properties.setProperty("person.name", person.getName());}properties.store(outputMessage.getBody(), "write by server");}
}

2、添加MappingPropertiesHttpMessageConverter

package com.cnbi.sourceread.mvc.converter;import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import java.util.List;/*** @ClassName ConsumerMvcConfiger* @Description 自定义MVC配置* @Author Wangjunkai* @Date 2021/2/20 23:39**/
@Configuration
public class ConsumerMvcConfiger extends WebMvcConfigurationSupport {@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {converters.add(new MappingPropertiesHttpMessageConverter());addDefaultHttpMessageConverters(converters);}
}

博主个人微信

HttpMessageConverter相关推荐

  1. restTemplate http请求报错:no suitable HttpMessageConverter found for response type and content type

    报错信息: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no sui ...

  2. HttpMessageConverter转换类型

    2019独角兽企业重金招聘Python工程师标准>>> HttpMessageConverter接口定义 * Strategy interface that specifies a ...

  3. springMVC 处理json 及 HttpMessageConverter 接口

    一.SpringMVC处理json的使用 1.添加依赖jar包 <dependency><groupId>com.fasterxml.jackson.core</grou ...

  4. 一篇文章教你弄懂SpringMvc中的HttpMessageConverter

    文章目录 一.HttpMessageConverter介绍 二.自定义HttpMessageConverter 写在前面: 我是「境里婆娑」.我还是从前那个少年,没有一丝丝改变,时间只不过是考验,种在 ...

  5. Spring MVC--自定义HttpMessageConverter接收JSON格式的数据

    替换默认的Jackson,使用fastjson来处理json格式的数据. index.jsp <%@ page language="java" contentType=&qu ...

  6. 使用HttpMessageConverter实现HTTP的序列化和反序列化

    对象的序列化/反序列化大家应该都比较熟悉:序列化就是将object转化为可以传输的二进制,反序列化就是将二进制转化为程序内部的对象.序列化/反序列化主要体现在程序I/O这个过程中,包括网络I/O和磁盘 ...

  7. 小小涉及OpenFeign原理:Could not extract response: no suitable HttpMessageConverter found for response type

    一.问题解释(想看总结的去最下面) org.springframework.web.client.UnknownContentTypeException: Could not extract resp ...

  8. java注解返回不同消息,SpringMVC源码剖析5:消息转换器HttpMessageConverter与@ResponseBody注解...

    转自 [SpringMVC关于json.xml自动转换的原理研究[附带源码分析]](https://www.cnblogs.com/fangj... 部分代码会放在我的的Github:https:// ...

  9. no suitable HttpMessageConverter found for request type [java.lang.Integer]

    今天在使用Spring Template的时候遇到了这个异常: no suitable HttpMessageConverter found for request type [java.lang.I ...

  10. 【Spring】No suitable HttpMessageConverter repsonse type

    文章目录 1.概述 2. 场景2 1.概述 一个flink项目,然后我修改了一下报错了,错误如下 Caused by: feign.codec.DecodeException: Could not e ...

最新文章

  1. jQuery遍历(1)
  2. iOS之深入解析Runtime的objc_msgSend“快速查找”底层原理
  3. when is this.oModel in sap-ui-core.js initialized for navigation working case
  4. 高仿美团钱包首页CollapsingToolbarLayout监听滑动隐藏效果(公司项目)
  5. java生成大素数_代码优化 - 生成素数
  6. 大岩俊之实用性阅读指南pdf_《实用性阅读指南》:二八法则、笔记法......开启你的阅读技能...
  7. linux tomcat 改端口号,Ubuntu中Tomcat更改80端口
  8. VB.Command()的参数
  9. promise用法_【JavaScript 教程】异步操作——Promise 对象
  10. 【Java从0到架构师】SpringBoot - 页面模版_Thymeleaf
  11. 16.2.1 连接到 SMTP 服务器
  12. 本科计算机的逻辑学,逻辑学本科专业介绍
  13. 简单二叉树的模型和解法(C++)
  14. OPNsense - 多功能高可靠易使用的防火墙(二)
  15. PL3369C原边12W电源芯片
  16. Oracle中Start With的用法
  17. 031--python--打印机票页面
  18. [HDSC] 华大Cortex-M离线烧录器(CM PGM)HCTL-1A使用方法记录
  19. 2022-2028全球与中国企业上云市场现状及未来发展趋势
  20. 分析器错误信息:nbsp;无法识别的配置节…

热门文章

  1. android导出txt文件格式,安卓端数据导出成txt文件
  2. Ben Fisch等联创爆出道德、法律丑闻,Espresso Systems风雨飘摇
  3. 从 Discord 看未来社交的「超级群」模式
  4. PHP 发送邮件配置 转发自http://www.cnblogs.com/dee0912/p/5448350.html
  5. 【Linux】set ff=unix
  6. 基于oracle的餐馆评价系统,美团大众点评优化评价系统 助力餐饮商家做好口碑营销...
  7. 微信公众号关键词自动回复文件设置教程
  8. arcgis之合并碎小图斑到相邻大块图斑
  9. reac-hook的使用
  10. RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405