5、自定义消息转换器

当我们想自定义消息转换器时,也不要着急,springboot中也定义了对应的接口,只要我们实现该接口就行了

public interface HttpMessageConverter<T> {boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);List<MediaType> getSupportedMediaTypes();default List<MediaType> getSupportedMediaTypes(Class<?> clazz) {return (canRead(clazz, null) || canWrite(clazz, null) ?getSupportedMediaTypes() : Collections.emptyList());}T read(Class<? extends T> clazz, HttpInputMessage inputMessage)throws IOException, HttpMessageNotReadableException;void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)throws IOException, HttpMessageNotWritableException;}

下面是具体的实现类:

package com.boot.config;import com.boot.pojo.People;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;public class PeopleMessageConvert implements HttpMessageConverter<People>  {//是否能够读取,意思就是能不能读取网络上的数据类型@Overridepublic boolean canRead(Class<?> clazz, MediaType mediaType) {return false;}//表示是否能够将数据改为对应格式@Overridepublic boolean canWrite(Class<?> clazz, MediaType mediaType) {return true;}//获取支持的媒体类型@Overridepublic List<MediaType> getSupportedMediaTypes() {return MediaType.parseMediaTypes("application/xyj-maiDang");}//读取@Overridepublic People read(Class<? extends People> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {return null;}//写出@Overridepublic void write(People people, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {String message=people.toString();OutputStream out = outputMessage.getBody();out.write(message.getBytes());}
}

关于媒体类型的自定义:

 public List<MediaType> getSupportedMediaTypes() {return MediaType.parseMediaTypes("application/xyj-maiDang");}

必须是按* / * 的格式,原因如下:

看到第6个就什么都明白了,是不是

然后就是将自定义的消息转换器添加到容器中,

//来自WebMvcConfigurer.java
default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {}default void extendMessageConverters(List<HttpMessageConverter<?>> converters) {}

这里我们使用的是extendMessageConverters方法因为这个方法是用于扩展的,他不会改变原来已经存在的转换器,

configureMessageConverters是配置转换器,这样会使容器中以前配置的转换器删除

package com.boot.config;import com.boot.pojo.People;
import com.boot.pojo.Pet;
import org.springframework.beans.PropertyEditorRegistrySupport;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;import java.util.List;//
@Configuration()
//@EnableConfigurationProperties(Pet.class)
public class MyConfig {//    @Bean
//    public Pet cat(){//        return new Pet();
//    }@Beanpublic WebMvcConfigurer webMvcConfigurer(){return new WebMvcConfigurer() {@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {converters.add(new PeopleMessageConvert());}};}}

然后debug就可以看见我们自定义的转换器已经被添加进去了

学习springboot,可以多看看WebMvcAutoConfiguration.java这个类,我们所需要的很多扩展都可以参考这个

springboot2------自定义消息转换器相关推荐

  1. SpringBoot___自定义消息转换器、MVC配置

    2019独角兽企业重金招聘Python工程师标准>>> 1. 自动配置的消息转换器   在SptingBoot的源码中的spring-boot-autoconfig的Jar包下,我们 ...

  2. spring boot处理请求返回值的格式(自定义消息转换器)

    springboot 将对象转化成json对象返回给前端,是通过多个消息转换器配合完成的 但是有些时候,默认的转化格式未必符合我们的要求,这个时候就需要进行自定义消息转换器 只需要在@Configur ...

  3. spring 自定义消息转换器

    消息转换器,顾名思义就是对返回的消息,进行转换.下面常见的例子如下: Spring MVC框架中,将HTTP请求信息转换为一个对象(@RequestBody注解),将对象输出为HTTP响应信息(@Re ...

  4. SpringBoot添加自定义消息转换器

    首先我们需要明白一个概念:springboot中很多配置都是使用了条件注解进行判断一个配置或者引入的类是否在容器中存在,如果存在会如何,如果不存在会如何. 也就是说,有些配置会在springboot中 ...

  5. springboot自定义消息转换器HttpMessageConverter

    在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换,底层这种灵活的消息转换机制就是利用HttpMessageCo ...

  6. SpringMvc自定义消息转换器

    SpringMvc配置文件代码 对于定义的消息转换器 必须通过 mvc:annotation-driven进行注册 消息转换器  会对请求的mini类型进行匹配 如果无法匹配 不会进行消息转换 < ...

  7. SpringBoot2.1.5(23)---SpringBoot 开发WEB应用

    SpringBoot非常适合Web应用程序开发.您可以使用嵌入式Tomcat.Jetty.Undertow或Netty创建一个独立的HTTP服务器.大多数Web应用程序使用Spring Boot St ...

  8. SpringMVC自定义配置消息转换器踩坑总结

    问题描述 最近在开发时候碰到一个问题,springmvc页面向后台传数据的时候,通常我是这样处理的,在前台把数据打成一个json,在后台接口中使用@requestbody定义一个对象来接收,但是这次数 ...

  9. SpringBoot消息转换器:HttpMessageConverter

    文章目录 消息转化器的作用 消息转化器的主要方法 默认配置的消息转化器 注意事项 在整个数据流转过程中,前端的请求报文转化为Java对象,Java对象转化为响应报文,这里就用到了HttpMessage ...

最新文章

  1. linux下screen工具使用
  2. 少儿编程语言python-2019儿童编程语言大全
  3. java的注释规范_Java代码注释规范
  4. Spring管理事务的若干配置形式
  5. 0311互联网新闻 | 知乎增加“视频回答”入口;苹果将最早于今年年底生产AR设备...
  6. 无线网状网、Zigbee、RFID三种技术分析
  7. extjs 基础部分
  8. zabbix监控redis
  9. HDU3791 二叉搜索树【二叉搜索树】
  10. matlab分析矩阵与线性变换
  11. CString转char*的两种方法讨论
  12. linux 分区100g整数,160GB硬盘双系统整数分区推荐方案
  13. 项目答辩演讲稿(详细原文)
  14. 【Unity】打包WebGL项目遇到的问题及解决记录
  15. Calibre Web 中阅读 epub 电子书时的夜间模式解决方案
  16. java实现调用打印机
  17. 使用MPICH构建一个四节点的集群系统
  18. 2015年智能家居大事记 合纵连横成主旋律
  19. 国外5个在网页设计最具影响力的人物
  20. 29、ZigBee 开发教程之基础篇—RFID 射频卡

热门文章

  1. 网络编程原理进阶___TCP/IP(javaee)
  2. 飞行棋游戏代码(C#)
  3. 手机芯片研发有多难_手机芯片设计很简单?详解手机芯片设计的难点
  4. 快捷指令,自动化脚本工具
  5. 区级医院计算机专业职称评审,医院职称晋升程序以及医生各级职称评审要求
  6. 按自己的需要获取对象中的属性
  7. 用python制作一张简单的节日贺卡
  8. css flex所有属性总结
  9. 复盘:购物中心这个生态
  10. Bootstrap typeahead自动补全插件的坑