com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4 Java Examples

The following examples show how to use com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebConfigurer.java    From mySpringBoot with Apache License 2.0 6 votes vote down vote up
/**
 * 修改自定义消息转换器
 *
 * @param converters
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    converter.setSupportedMediaTypes(getSupportedMediaTypes());
    FastJsonConfig config = new FastJsonConfig();
    config.setSerializerFeatures(
            // String null -> ""
            SerializerFeature.WriteNullStringAsEmpty,
            // Number null -> 0
            SerializerFeature.WriteNullNumberAsZero,
            //禁止循环引用
            SerializerFeature.DisableCircularReferenceDetect
    );
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
 
Example #2
Source File: FastJsonHttpMessageConvertersConfiguration.java    From wenku with MIT License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean({ FastJsonHttpMessageConverter4.class })
//当没有注册这个类时,自动注册Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();

    //使用最新的官方推荐配置对象的方式来注入fastjson的序列化特征
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullStringAsEmpty
     );
    
    //添加对json值的过滤,因为像移动APP,服务端在传json值时最好不要传null,而是使用“”,这是一个演示
    ValueFilter valueFilter = new ValueFilter() {//5
        //o 是class
        //s 是key值
        //o1 是value值
        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setSerializeFilters(valueFilter);

    converter.setFastJsonConfig(fastJsonConfig);

    return converter;
}
 
Example #3
Source File: JsonResultValueHandler.java    From app-engine with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
    return FastJsonHttpMessageConverter4.class.isAssignableFrom(converterType);
}