Java Code Examples for com.alibaba.fastjson.support.config.FastJsonConfig#setSerializerFeatures()

The following examples show how to use com.alibaba.fastjson.support.config.FastJsonConfig#setSerializerFeatures() . 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: WebMvcConfig.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter httpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
            SerializerFeature.WriteEnumUsingToString,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteDateUseDateFormat);
    fastJsonConfig.setSerializeFilters(new ValueFilter() {
        @Override
        public Object process(Object o, String s, Object source) {
            if (source == null) {
                return "";
            }
            if (source instanceof Date) {
                return ((Date) source).getTime();
            }
            return source;
        }
    });
    httpMessageConverter.setFastJsonConfig(fastJsonConfig);
    converters.add(httpMessageConverter);
}
 
Example 2
Source File: WebMvcConfigurer.java    From mysiteforme with Apache License 2.0 6 votes vote down vote up
/**
 *  fastjson序列化
 *
 * */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    List<MediaType> supportedMediaTypes = new ArrayList<>();
    supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");    // 自定义时间格式
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullBooleanAsFalse,SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.BrowserCompatible,SerializerFeature.WriteNonStringKeyAsString);
    converters.add(fastJsonHttpMessageConverter);
    converters.add(responseBodyConverter());
}
 
Example 3
Source File: WebMvcConfig.java    From littleca with Apache License 2.0 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //1.需要定义一个convert转换消息的对象;
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    //3处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON);
    fastJsonConfig.setCharset(Charset.forName("UTF-8"));
    //4.在convert中添加配置信息.
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    //5.将convert添加到converters当中.
    converters.add(1,fastJsonHttpMessageConverter);
}
 
Example 4
Source File: CustomMVCConfiguration.java    From spring-boot-starter-netty with GNU General Public License v3.0 6 votes vote down vote up
@Bean
FastJsonHttpMessageConverter getFastJsonConv() {
    FastJsonHttpMessageConverter conv = new FastJsonHttpMessageConverter();

    FastJsonConfig oFastJsonConfig = new FastJsonConfig();
    oFastJsonConfig.setSerializerFeatures(
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteDateUseDateFormat
    );
    conv.setFastJsonConfig(oFastJsonConfig);

    List<MediaType> types = new ArrayList<>();
    types.add(MediaType.APPLICATION_JSON_UTF8);
    types.add(MediaType.APPLICATION_JSON);
    conv.setSupportedMediaTypes(types);
    return conv;
}
 
Example 5
Source File: BeanConfig.java    From xmfcn-spring-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * 替换springMVC JSON格式化工具为FastJSON,同时支持输出value=null的字段
 *
 * @return http message converters
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    //1.需要定义一个convert转换消息的对象;
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    //2:添加fastJson的配置信息;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
    fastJsonConfig.setSerializeFilters();
    //3处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    //4.在convert中添加配置信息.
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastJsonHttpMessageConverter);

}
 
Example 6
Source File: FastjsonConfiguration.java    From seed with Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverters fastjsonConverter(){
    List<SerializerFeature> serializerFeatureList = new ArrayList<>();
    serializerFeatureList.add(SerializerFeature.PrettyFormat);
    serializerFeatureList.add(SerializerFeature.QuoteFieldNames);
    serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
    serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
    serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
    serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
    SerializerFeature[] serializerFeatures = new SerializerFeature[serializerFeatureList.size()];
    serializerFeatureList.toArray(serializerFeatures);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonConfig.setSerializerFeatures(serializerFeatures);
    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}
 
Example 7
Source File: RedisConfig.java    From sophia_scaffolding with Apache License 2.0 6 votes vote down vote up
/**
 *  使用@Bean注入fastJsonHttpMessageConvert,fastJson序列化
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
    // 1、需要先定义一个 convert 转换消息的对象;
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

    //2-1 处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    fastConverter.setSupportedMediaTypes(fastMediaTypes);

    //3、在convert中添加配置信息.
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}
 
Example 8
Source File: FastJsonConfiguration.java    From momo-cloud-permission with Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverters fastJsonConfigure(){
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    //日期格式化
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    //修改配置返回内容的过滤
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature. WriteMapNullValue,//是否输出值为null的字段,默认为false
            SerializerFeature.WriteDateUseDateFormat,//格式化标签
    		SerializerFeature.WriteNullListAsEmpty , //List字段如果为null,输出为[],而非null
            SerializerFeature.DisableCircularReferenceDetect,//消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
            SerializerFeature.WriteMapNullValue,//是否输出值为null的字段,默认为false。
            SerializerFeature.WriteNullStringAsEmpty,//字符类型字段如果为null,输出为"",而非null
            SerializerFeature.WriteNullBooleanAsFalse//Boolean字段如果为null,输出为false,而非null
    );
    converter.setFastJsonConfig(fastJsonConfig);
    //处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    converter.setSupportedMediaTypes(fastMediaTypes);
    converter.setFastJsonConfig(fastJsonConfig);
    //将fastjson添加到视图消息转换器列表内
    return new HttpMessageConverters(converter);
}
 
Example 9
Source File: FastJsonConfiguration.java    From momo-cloud-permission with Apache License 2.0 6 votes vote down vote up
@Bean
    public HttpMessageConverters fastJsonConfigure(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //日期格式化
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //修改配置返回内容的过滤
        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature. WriteMapNullValue,//是否输出值为null的字段,默认为false
                SerializerFeature.WriteDateUseDateFormat//格式化标签
//        		SerializerFeature.WriteNullListAsEmpty  ,//List字段如果为null,输出为[],而非null
//                SerializerFeature.DisableCircularReferenceDetect,//消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
//                SerializerFeature.WriteMapNullValue,//是否输出值为null的字段,默认为false。
//                SerializerFeature.WriteNullStringAsEmpty//字符类型字段如果为null,输出为"",而非null
//                SerializerFeature.WriteNullBooleanAsFalse//Boolean字段如果为null,输出为false,而非null
        );
        converter.setFastJsonConfig(fastJsonConfig);
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(fastMediaTypes);
        converter.setFastJsonConfig(fastJsonConfig);
        //将fastjson添加到视图消息转换器列表内
        return new HttpMessageConverters(converter);
    }
 
Example 10
Source File: FastjsonConfig.java    From spring-boot-demo-all with Apache License 2.0 6 votes vote down vote up
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.PrettyFormat,
            SerializerFeature.WriteClassName,
            SerializerFeature.WriteMapNullValue
    );
    List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);

    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    ValueFilter valueFilter = new ValueFilter() {
        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setSerializeFilters(valueFilter);
    converter.setSupportedMediaTypes(fastMediaTypes);
    converter.setFastJsonConfig(fastJsonConfig);
    return converter;
}
 
Example 11
Source File: SiteConfiguration.java    From mblog with GNU General Public License v3.0 5 votes vote down vote up
@Bean
@ConditionalOnClass({JSON.class})
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.DisableCircularReferenceDetect
    );
    fastConverter.setFastJsonConfig(fastJsonConfig);
    return fastConverter;
}
 
Example 12
Source File: Application.java    From AthenaServing with Apache License 2.0 5 votes vote down vote up
/**
 * fastjson 转换器
 *
 * @param converters
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	super.configureMessageConverters(converters);
	FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
	FastJsonConfig fastJsonConfig = new FastJsonConfig();
	fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
	List<MediaType> fastMediaTypes = new ArrayList<>();
	fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
	fastConverter.setSupportedMediaTypes(fastMediaTypes);
	fastConverter.setFastJsonConfig(fastJsonConfig);
	converters.add(fastConverter);
}
 
Example 13
Source File: HttpMessageConverterAutoConfiguration.java    From api-boot with Apache License 2.0 5 votes vote down vote up
/**
 * http message converter fastjson实现实例
 * 通过fastjson方式进行格式化返回json字符串
 *
 * @return http message converter
 */
@Bean
@ConditionalOnMissingBean
HttpMessageConverters fastJsonHttpMessageConverters() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    //创建fastJson配置实体类
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteNullBooleanAsFalse
    );

    // get customer define value filters
    List<String> packages = AutoConfigurationPackages.get(beanFactory);
    // get plugin define value filters
    packages.addAll(Arrays.asList(VALUE_FILTER_PACKAGE));

    fastJsonConfig.setSerializeFilters(getDefineFilters(packages));

    fastConverter.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastConverter);
}
 
Example 14
Source File: Application.java    From bookmark with MIT License 5 votes vote down vote up
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    // 1.定义一个converters转换消息的对象
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
    // 3.在converter中添加配置信息
    fastConverter.setFastJsonConfig(fastJsonConfig);
    // 5.返回HttpMessageConverters对象
    return new HttpMessageConverters(fastConverter);
}
 
Example 15
Source File: ApplicationConfiguration.java    From spring-cloud-shop with MIT License 5 votes vote down vote up
@Bean
public HttpMessageConverters fastJsonConfigure() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    converter.setFastJsonConfig(fastJsonConfig);

    List<MediaType> supportedMediaTypes = new ArrayList<>();

    supportedMediaTypes.add(MediaType.APPLICATION_JSON);
    supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
    supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
    supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
    supportedMediaTypes.add(MediaType.APPLICATION_PDF);
    supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
    supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
    supportedMediaTypes.add(MediaType.APPLICATION_XML);
    supportedMediaTypes.add(MediaType.IMAGE_GIF);
    supportedMediaTypes.add(MediaType.IMAGE_JPEG);
    supportedMediaTypes.add(MediaType.IMAGE_PNG);
    supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
    supportedMediaTypes.add(MediaType.TEXT_HTML);
    supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
    supportedMediaTypes.add(MediaType.TEXT_PLAIN);
    supportedMediaTypes.add(MediaType.TEXT_XML);
    converter.setSupportedMediaTypes(supportedMediaTypes);
    return new HttpMessageConverters(converter);
}
 
Example 16
Source File: WebConfig.java    From SpringBoot-Home with Apache License 2.0 5 votes vote down vote up
/**
 * 启用 FastJson: https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson
 * @param converters
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
    //自定义配置...
    FastJsonConfig config = new FastJsonConfig();
    //- https://github.com/alibaba/fastjson/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
    //- 不用 WriteDateUseDateFormat ,日期由客户端自行格式化
    //- 不用 WriteMapNullValue ,默认不输出属性值为null的字段
    //- BrowserCompatible ,会把中文编码为Unicode转义字符,需要的时候可以加上
    config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
    converter.setFastJsonConfig(config);
    converters.add(0, converter);
}
 
Example 17
Source File: MvcConfigurer.java    From spring-boot-api-project-seed with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //使用阿里 FastJson 作为JSON MessageConverter
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig config = new FastJsonConfig();
    //保留空的字段
    config.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
    // 按需配置,更多参考FastJson文档
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
 
Example 18
Source File: WebConfig.java    From easyweb with Apache License 2.0 5 votes vote down vote up
/**
 * 配置使用springmvc fastjson
 * @return
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}
 
Example 19
Source File: FastjsonConfiguration.java    From seed with Apache License 2.0 4 votes vote down vote up
@Bean
public HttpMessageConverters fastjsonConverter(){
    List<SerializerFeature> serializerFeatureList = new ArrayList<>();
    //格式化输出(默认只会输出成一行的字符串)
    serializerFeatureList.add(SerializerFeature.PrettyFormat);
    //输出key时是否使用双引号,默认为true
    serializerFeatureList.add(SerializerFeature.QuoteFieldNames);
    //是否输出值为null的字段,默认为true
    serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
    //List字段如果为null,输出为[],而非null
    serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
    //数值字段如果为null,输出为0,而非null
    serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
    //字符类型字段如果为null,输出为"",而非null
    serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
    //Boolean字段如果为null,输出为false,而非null
    serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
    //使用默认的日期格式[yyyy-MM-dd HH:mm:ss]输出Date类型,未指定该属性则会将java.util.Date类型输出为1484030642746
    serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
    ////测试发现,无论是否设置该属性,都会输出:["age":0]、["age":123.456]
    //serializerFeatureList.add(SerializerFeature.WriteBigDecimalAsPlain);
    ////设置该属性会使得在输出时以字符串来输出非字符串的值,比如["age":"0"]、["abc":"false"],但是List不是这样,还是会输出["goodsList":[]]
    //serializerFeatureList.add(SerializerFeature.WriteNonStringValueAsString);
    ////假设序列化的实体类为com.jadyer.demo.open.model.ReqData,设置该属性会使得输出的json中增加一个key=["@type":"com.jadyer.demo.open.model.ReqData",]
    //serializerFeatureList.add(SerializerFeature.WriteClassName);
    ////设置该属性会使得序列化时增加特殊处理字符处理,比如原本的["mytime":"2017-01-10 14:56:41",]会输出为["mytime":"2017\u002D01\u002D10\u002014\u003A56\u003A41",]
    //serializerFeatureList.add(SerializerFeature.BrowserSecure);
    SerializerFeature[] serializerFeatures = new SerializerFeature[serializerFeatureList.size()];
    serializerFeatureList.toArray(serializerFeatures);

    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    //Fastjson默认就是UTF-8
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    //Fastjson默认就是[yyyy-MM-dd HH:mm:ss]
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    //设置序列化输出时的一些额外属性
    fastJsonConfig.setSerializerFeatures(serializerFeatures);

    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}
 
Example 20
Source File: FastJsonProvider.java    From uavstack with Apache License 2.0 4 votes vote down vote up
/**
     * Method that JAX-RS container calls to serialize given value.
     */
    public void writeTo(Object obj, //
                        Class<?> type, //
                        Type genericType, //
                        Annotation[] annotations, //
                        MediaType mediaType, //
                        MultivaluedMap<String, Object> httpHeaders, //
                        OutputStream entityStream //
    ) throws IOException, WebApplicationException {

        FastJsonConfig fastJsonConfig = locateConfigProvider(type, mediaType);

        SerializerFeature[] serializerFeatures = fastJsonConfig.getSerializerFeatures();
        if (pretty) {
            if (serializerFeatures == null)
                serializerFeatures = new SerializerFeature[]{SerializerFeature.PrettyFormat};
            else {
                List<SerializerFeature> featureList = new ArrayList<SerializerFeature>(Arrays
                        .asList(serializerFeatures));
                featureList.add(SerializerFeature.PrettyFormat);
                serializerFeatures = featureList.toArray(serializerFeatures);
            }
            fastJsonConfig.setSerializerFeatures(serializerFeatures);
        }

        try {
            int len = JSON.writeJSONString(entityStream, //
                    fastJsonConfig.getCharset(), //
                    obj, //
                    fastJsonConfig.getSerializeConfig(), //
                    fastJsonConfig.getSerializeFilters(), //
                    fastJsonConfig.getDateFormat(), //
                    JSON.DEFAULT_GENERATE_FEATURE, //
                    fastJsonConfig.getSerializerFeatures());

//            // add Content-Length
//            if (fastJsonConfig.isWriteContentLength()) {
//                httpHeaders.add("Content-Length", String.valueOf(len));
//            }

            entityStream.flush();

        } catch (JSONException ex) {

            throw new WebApplicationException("Could not write JSON: " + ex.getMessage(), ex);
        }
    }