SpringBoot配置HttpMessageConverters

SpringBoot配置HttpMessageConverters

自定义MessageConverters

package com.haixiangpuhui.credit.web.warehouse.consumer.config.msgconvert;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;

/**
 * 配置阿里巴巴 httpMessageConverters
 * @author HX-011
 *
 */
@Configuration
public class FastJsonHttpMessageConvertersConfig extends WebMvcConfigurerAdapter {

	@Bean
	public FastJsonConfig fastJsonConfig() {
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		SerializerFeature writeMapNullValue = SerializerFeature.WriteMapNullValue;
		SerializerFeature WriteNullStringAsEmpty = SerializerFeature.WriteNullStringAsEmpty;
		SerializerFeature WriteNullNumberAsZero = SerializerFeature.WriteNullNumberAsZero;
		SerializerFeature WriteNullListAsEmpty = SerializerFeature.WriteNullListAsEmpty;
		fastJsonConfig.setSerializerFeatures(writeMapNullValue, WriteNullStringAsEmpty, 
				WriteNullNumberAsZero, WriteNullListAsEmpty);
		return fastJsonConfig;
	}

	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters(
			@Qualifier("fastJsonConfig") FastJsonConfig fastJsonConfig) {
		FastJsonHttpMessageConverter4 fastConverter = new FastJsonHttpMessageConverter4();
		fastConverter.setFastJsonConfig(fastJsonConfig);
		HttpMessageConverter converter = fastConverter;
		return new HttpMessageConverters(converter);
	}
}

你可能感兴趣的:(SpringBoot)