首页 > 其他分享 >spring,springBoot配置类型转化器Converter以及FastJsonHttpMessageConverter,StringHttpMessageConverter 使用

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

时间:2023-03-23 10:06:43浏览次数:33  
标签:XML FastJsonHttpMessageConverter springBoot StringHttpMessageConverter JSON 转换器 

 

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了


 



标签:XML,FastJsonHttpMessageConverter,springBoot,StringHttpMessageConverter,JSON,转换器,
From: https://blog.51cto.com/u_15535797/6144362

相关文章