• spring,springBoot配置类型转化器Converter以及FastJsonHttpMessageConverter,StringHttpMessageConverter 使用


    转载请注明出处: 

      https://i.cnblogs.com/posts/edit;postId=14045507

    spring,spring boot 等框架项目通过@RequestBody,@ResponseBody 完成请求报文到响应对象及响应对象到响应报文的转换,其底层是通过

    消息转换器完成消息之间的转换,包括格式转化,类型转化等。比如返回JSON数据或XML数据等。

      spring 默认有很多消息类型转换器:

        MappingJackson2XmlHttpMessageConverter 基于Jackson的XML转换器,能够将对象转换成XML格式的数据
        MappingJackson2HttpMessageConverter 基于 Jackson 的JSON转换器,能够将对象转换成JSON格式的数据
        GsonHttpMessageConverter 基于Gson的JSON转换器,能够将对象转换成JSON格式数据

      对于系统中默认包含的转换器,只要我们在项目中加入转换器所依赖的JAR包,相关转换器就会被加载。

    @RequestMapping(value="/json", produces={"application/json; charset=UTF-8"})

      使用@RequestMapping设置的路径设置为 value属性的值,此外另外设置一个属性 produces,这个属性接受一个字符串数组。接受的数据类型是 media type。上面这个例子就是标明这个方法的返回结果要转换成UTF-8编码的JSON数据。

      SpringMVC在项目初始化时,会去扫描系统中的JAR包,然后根据扫描到的JAR包设置默认的转换类型,大概的扫描过程是:
        1)检查系统中是否存在jackson-xml的JAR包,如果存在,就将数据转换类型列表中设置XML类型,以及其对应的转换器
        2)检查系统中是否存在jackson-json的JAR包,如果存在,就在数据转换类型列表中设置JSON类型,以及其对应的转换器

      因为是先检测的XML,因此XML排在JSON前面,如果系统两者的JAR包都存在,那么默认情况下数据会被转换成XML格式

    @RequestMapping(value="/xml", produces={"application/xml; charset=UTF-8"})
    @ResponseBody

      使用以上注解,则将会响应转换为XML 数据格式。

    我们也可以在项目中使用指定的消息类型转换器,FastJson 封装了对应的类型转换器,只要在项目中引用fastJson对应的依赖配置;

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.31</version>
            </dependency>     

      通过实现 WebMvcConfigurer 接口来配置指定的消息类型转换器,

        WebMvcConfigurer接口其实是spring的一种内部配置方式,采用java Bean的形式代替传统的xml配置形式,可以自定义一些Handler,Interceptor,ViewResolver, MessageConverter。基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口;

      springboot2.0版本以后推荐使用这种方式来进行web配置,这样不会覆盖掉springboot的一些默认配置。配置类如下:

    package com.lf.mp.test;

    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.converter.StringHttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

    import java.nio.charset.Charset;
    import java.util.ArrayList;
    import java.util.List;

    @Configuration
    public class CustomHttpMessageConverter implements WebMvcConfigurer {

    /**
    * 自定义使用FastJsonHttpMessageConverter
    *
    * @return
    */
    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
    SerializerFeature.QuoteFieldNames,
    SerializerFeature.WriteMapNullValue,//保留空的字段
    SerializerFeature.WriteNullListAsEmpty,//List null-> []
    SerializerFeature.WriteDateUseDateFormat,// 日期格式化
    SerializerFeature.WriteNullStringAsEmpty);//String null -> ""

    List<MediaType> mediaTypeList = new ArrayList<>();
    mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
    mediaTypeList.add(MediaType.APPLICATION_JSON);
    fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypeList);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    return fastJsonHttpMessageConverter;
    }

    /**
    * 在RsponseBody注解下,Spring处理返回值为String时会用到StringHttpMessageConverter,我们只需要在配置文件中设置好他的编译编码就ok了
    *
    * @return
    */
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
    StringHttpMessageConverter httpMessageConverter = new StringHttpMessageConverter();
    httpMessageConverter.setDefaultCharset(Charset.defaultCharset());
    return httpMessageConverter;
    }

    //保证StringHttpMessageConverter在FastJsonHttpMessageConverter前被调用
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    //converters.removeIf(t -> t instanceof MappingJackson2HttpMessageConverter);
    converters.clear();
    StringHttpMessageConverter converter = new StringHttpMessageConverter(
    Charset.forName("UTF-8"));
    converters.add(converter);
    converters.add(fastJsonHttpMessageConverter());
    }
    }

    有两点需要注意: 

     1.如果没有配置MediaType.APPLICATION_JSON_UTF8,默认值是MediaType.ALL,FastJsonHttpMessageConverter会去处理消息格式为"text/html;charset=UTF-8"

     2.在RsponseBody注解下,Spring处理返回值为String时会用到StringHttpMessageConverter,我们只需要在配置文件中设置好他的编译编码就ok了

    如果想了解StringHttpMessageConverter的使用,可以看这篇博客:https://blog.csdn.net/x_iya/article/details/77872173

  • 相关阅读:
    bugKu getshell
    XCTF 进阶区 CAT
    php弱类型比较
    XCTF command_execution
    关于错误 openssl/ssl.h:没有那个文件或目录的解决办法
    libffi-dev : 依赖: libffi6 (= 3.2.1-4) 但是 3.2.1-4kord 正要被安装
    如何查看 Ubuntu下已安装包版本号
    git 下载指定tag版本的源码
    ubuntu 环境 openstack 源码包制成 deb 包
    fedora 国内源
  • 原文地址:https://www.cnblogs.com/zjdxr-up/p/14045507.html
Copyright © 2020-2023  润新知