HttpMessageConverter
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相关推荐
- restTemplate http请求报错:no suitable HttpMessageConverter found for response type and content type
报错信息: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no sui ...
- HttpMessageConverter转换类型
2019独角兽企业重金招聘Python工程师标准>>> HttpMessageConverter接口定义 * Strategy interface that specifies a ...
- springMVC 处理json 及 HttpMessageConverter 接口
一.SpringMVC处理json的使用 1.添加依赖jar包 <dependency><groupId>com.fasterxml.jackson.core</grou ...
- 一篇文章教你弄懂SpringMvc中的HttpMessageConverter
文章目录 一.HttpMessageConverter介绍 二.自定义HttpMessageConverter 写在前面: 我是「境里婆娑」.我还是从前那个少年,没有一丝丝改变,时间只不过是考验,种在 ...
- Spring MVC--自定义HttpMessageConverter接收JSON格式的数据
替换默认的Jackson,使用fastjson来处理json格式的数据. index.jsp <%@ page language="java" contentType=&qu ...
- 使用HttpMessageConverter实现HTTP的序列化和反序列化
对象的序列化/反序列化大家应该都比较熟悉:序列化就是将object转化为可以传输的二进制,反序列化就是将二进制转化为程序内部的对象.序列化/反序列化主要体现在程序I/O这个过程中,包括网络I/O和磁盘 ...
- 小小涉及OpenFeign原理:Could not extract response: no suitable HttpMessageConverter found for response type
一.问题解释(想看总结的去最下面) org.springframework.web.client.UnknownContentTypeException: Could not extract resp ...
- java注解返回不同消息,SpringMVC源码剖析5:消息转换器HttpMessageConverter与@ResponseBody注解...
转自 [SpringMVC关于json.xml自动转换的原理研究[附带源码分析]](https://www.cnblogs.com/fangj... 部分代码会放在我的的Github:https:// ...
- no suitable HttpMessageConverter found for request type [java.lang.Integer]
今天在使用Spring Template的时候遇到了这个异常: no suitable HttpMessageConverter found for request type [java.lang.I ...
- 【Spring】No suitable HttpMessageConverter repsonse type
文章目录 1.概述 2. 场景2 1.概述 一个flink项目,然后我修改了一下报错了,错误如下 Caused by: feign.codec.DecodeException: Could not e ...
最新文章
- jQuery遍历(1)
- iOS之深入解析Runtime的objc_msgSend“快速查找”底层原理
- when is this.oModel in sap-ui-core.js initialized for navigation working case
- 高仿美团钱包首页CollapsingToolbarLayout监听滑动隐藏效果(公司项目)
- java生成大素数_代码优化 - 生成素数
- 大岩俊之实用性阅读指南pdf_《实用性阅读指南》:二八法则、笔记法......开启你的阅读技能...
- linux tomcat 改端口号,Ubuntu中Tomcat更改80端口
- VB.Command()的参数
- promise用法_【JavaScript 教程】异步操作——Promise 对象
- 【Java从0到架构师】SpringBoot - 页面模版_Thymeleaf
- 16.2.1 连接到 SMTP 服务器
- 本科计算机的逻辑学,逻辑学本科专业介绍
- 简单二叉树的模型和解法(C++)
- OPNsense - 多功能高可靠易使用的防火墙(二)
- PL3369C原边12W电源芯片
- Oracle中Start With的用法
- 031--python--打印机票页面
- [HDSC] 华大Cortex-M离线烧录器(CM PGM)HCTL-1A使用方法记录
- 2022-2028全球与中国企业上云市场现状及未来发展趋势
- 分析器错误信息:nbsp;无法识别的配置节…
热门文章
- android导出txt文件格式,安卓端数据导出成txt文件
- Ben Fisch等联创爆出道德、法律丑闻,Espresso Systems风雨飘摇
- 从 Discord 看未来社交的「超级群」模式
- PHP 发送邮件配置 转发自http://www.cnblogs.com/dee0912/p/5448350.html
- 【Linux】set ff=unix
- 基于oracle的餐馆评价系统,美团大众点评优化评价系统 助力餐饮商家做好口碑营销...
- 微信公众号关键词自动回复文件设置教程
- arcgis之合并碎小图斑到相邻大块图斑
- reac-hook的使用
- RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405