基于FastJsonHttpMessageConverter




import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyPreFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import springfox.documentation.spring.web.json.Json;

import java.util.Collections;
import java.util.Objects;


//@Configuration

public class SerializerConvertConfig {

    @Bean
    public FastJsonHttpMessageConverter fastjsonConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(new SerializerFeature[]
                {         SerializerFeature.PrettyFormat,
                        SerializerFeature.WriteMapNullValue,
                        SerializerFeature.WriteNullStringAsEmpty,
                        SerializerFeature.WriteNullNumberAsZero,
                        SerializerFeature.WriteNullListAsEmpty,
                        SerializerFeature.WriteNullBooleanAsFalse,
                        SerializerFeature.WriteDateUseDateFormat,
                        SerializerFeature.BrowserCompatible,
                        SerializerFeature.DisableCircularReferenceDetect});
        PropertyPreFilter propertyPreFilter = (w, s, n) -> {
            if (s instanceof Json) {
                Json j = (Json) s;
                final String value = j.value();
                final JSONObject jsonObject = JSON.parseObject(value);
                w.write(jsonObject.get(jsonObject));
            }
            return true;
        };
        ValueFilter valueFilter = (o, s, o1) -> {
            if (Objects.isNull(o1)) {

              /*  if(s.equals("updateTime")||s.equals("createTime") )
                {

                }*/

                return "";
            }  else if (o1 instanceof Long) {
                return o1.toString();
            }
            else {

                return o1;
            }
        };
        fastJsonConfig.setSerializeFilters(valueFilter,propertyPreFilter);
        converter.setFastJsonConfig(fastJsonConfig);
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
        return converter;
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Fastjson是一个用于处理JSON数据的Java库,Fastjson2则是Fastjson的第二个版本。 Fastjson2提供了一个FastJsonHttpMessageConverter类,用于在Spring MVC中将Java对象转换为JSON数据。它可以将请求的JSON数据转换为Java对象,并将Java对象转换为响应的JSON数据。这个转换器可以方便地集成到Spring应用程序中。 要在Spring MVC中使用FastJsonHttpMessageConverter,你需要在配置文件中进行相应的配置。首先,你需要添加Fastjson的依赖项到你的项目中。然后,在Spring的配置文件中,通过添加以下代码来配置FastJsonHttpMessageConverter: ```java @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); converters.add(fastConverter); } @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { for (HttpMessageConverter<?> converter : converters) { if (converter instanceof MappingJackson2HttpMessageConverter) { MappingJackson2HttpMessageConverter jsonConverter = (MappingJackson2HttpMessageConverter) converter; jsonConverter.setObjectMapper(new ObjectMapper()); } } } } ``` 以上配置将FastJsonHttpMessageConverter添加到Spring MVC中,并将其作为默认的JSON转换器。你还可以根据需要进一步配置Fastjson的相关选项,比如日期格式化、类型处理等。 希望这能解答你关于Fastjson2和FastJsonHttpMessageConverter的问题!如有更多疑问,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值