java fastjson 设置全局输出name最小化

1.通过自定义Filter实现

https://github.com/alibaba/fastjson/wiki/SerializeFilter

public class JackJsonLowCaseFilter implements NameFilter {
    public JackJsonLowCaseFilter() {
    }

    @Override
    public String process(Object source, String name, Object value) {
        if (name != null && name.length() != 0) {
           return  name.toLowerCase();
        } else {
            return name;
        }
    }
}

 

2.设置全局输出

 @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        //1.需要定义一个convert转换消息的对象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializeFilters(new JackJsonLowCaseFilter());
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //3处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        //4.在convert中添加配置信息.
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        //5.将convert添加到converters当中.
        converters.add(fastJsonHttpMessageConverter);
    }

 

posted @ 2018-12-13 17:46  xiaocong_soft  阅读(867)  评论(1编辑  收藏  举报