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

The following examples show how to use com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter. 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: HttpBasicAuthConfig.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((HttpMessageConverter<?>) fastJsonHttpMessageConverter);

}
 
Example #2
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 #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: DefaultWebMvcConfigurerAdapter.java    From dk-foundation with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();

    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    JSON.defaultTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
    JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    fastJsonConfig.setSerializerFeatures(SerializerFeature.BrowserCompatible,
            SerializerFeature.BrowserSecure,
            SerializerFeature.PrettyFormat,
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.DisableCircularReferenceDetect);
    /**
     * 解决Long转json精度丢失的问题
     */
    SerializeConfig serializeConfig = SerializeConfig.globalInstance;
    serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
    serializeConfig.put(Long.class, ToStringSerializer.instance);
    serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
    fastJsonConfig.setSerializeConfig(serializeConfig);
    fastConvert.setFastJsonConfig(fastJsonConfig);
    converters.add(fastConvert);
}
 
Example #5
Source File: WorkerController.java    From DataLink with Apache License 2.0 6 votes vote down vote up
private String getJavaOpts(String workerId) {
    try {
        if (StringUtils.isNotEmpty(workerId)) {
            WorkerInfo workerInfo = workerService.getById(Long.valueOf(workerId));
            if (workerInfo != null) {
                String url = "http://" + workerInfo.getWorkerAddress() + ":" + workerInfo.getRestPort() + "/worker/getJavaOpts/" + workerId;

                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                HttpEntity request = new HttpEntity(null, headers);

                Map<String, String> result = new RestTemplate(Lists.newArrayList(new FastJsonHttpMessageConverter())).postForObject(url, request, Map.class);
                return result.get("content");
            }

            return null;
        }

    } catch (Exception e) {
        return e.getMessage();
    }

    return null;
}
 
Example #6
Source File: WorkerController.java    From DataLink with Apache License 2.0 6 votes vote down vote up
private String restart(String workerId) {
    if (StringUtils.isNotEmpty(workerId)) {
        WorkerInfo workerInfo = workerService.getById(Long.valueOf(workerId));
        if (workerInfo != null) {
            String url = "http://" + workerInfo.getWorkerAddress() + ":" + workerInfo.getRestPort() + "/worker/restartWorker/" + workerId;

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity request = new HttpEntity(null, headers);

            Map<String, String> result = new RestTemplate(Lists.newArrayList(new FastJsonHttpMessageConverter())).postForObject(url, request, Map.class);

            // TODO 重启完成后,记录此次的javaopts参数,添加到表t_dl_worker的字段parameters(定义一个类jvm_parameter.run_java_opts)
            // TODO 此处先不调整,原因是:rebalance时,应该获取worker当前正在运行的配置参数;

            AuditLogUtils.saveAuditLog(getAuditLogInfo(workerInfo, "001002009", AuditLogOperType.other.getValue()));
            return result.get("content");
        }

        return "fail";
    }

    return "fail";
}
 
Example #7
Source File: WorkerController.java    From DataLink with Apache License 2.0 6 votes vote down vote up
@ResponseBody
@RequestMapping(value = "/toEditLogback")
public String toLogback(HttpServletRequest requestPara) {
    String workerId = requestPara.getParameter("id");
    if (StringUtils.isBlank(workerId)) {
        return "fail";
    }
    try {
        WorkerInfo workerInfo = workerService.getById(Long.valueOf(workerId));
        if (workerInfo != null) {
            String url = "http://" + workerInfo.getWorkerAddress() + ":" + workerInfo.getRestPort() + "/worker/toEditLogback/" + workerId;
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity request = new HttpEntity(null, headers);
            Map<String, String> result = new RestTemplate(Lists.newArrayList(new FastJsonHttpMessageConverter())).postForObject(url, request, Map.class);
            return result.get("content");
        }
        return "fail";
    } catch (Exception e) {
        logger.info("request to edit logback.xml error:", e);
        return e.getMessage();
    }
}
 
Example #8
Source File: WorkerController.java    From DataLink with Apache License 2.0 6 votes vote down vote up
@ResponseBody
@RequestMapping(value = "/doEditLogback")
public String saveLogback(String workerId, String content) {
    logger.info("Receive a request to save logback.xml, with workerId " + workerId + "\r\n with content " + content);

    if (StringUtils.isBlank(content)) {
        return "content can not be null";
    }
    try {
        WorkerInfo workerInfo = workerService.getById(Long.valueOf(workerId));
        if (workerInfo != null) {
            String url = "http://" + workerInfo.getWorkerAddress() + ":" + workerInfo.getRestPort() + "/worker/doEditLogback/" + workerId;
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            Map<String, String> map = new HashMap<>();
            map.put("content", content);
            HttpEntity request = new HttpEntity(map, headers);
            new RestTemplate(Lists.newArrayList(new FastJsonHttpMessageConverter())).postForObject(url, request, String.class);
            return "success";
        }
        return "fail";
    } catch (Exception e) {
        logger.info("request to save logback.xml error:", e);
        return e.getMessage();
    }
}
 
Example #9
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 #10
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 #11
Source File: SmartRestTemplateConfig.java    From smart-admin with MIT License 6 votes vote down vote up
/**
 * fastJsonRestTemplate
 *
 * @return
 */
@Bean(name = "fastJsonRestTemplate")
public RestTemplate fastJsonRestTemplate() {
    RestTemplate restTemplate = new RestTemplate(httpRequestFactory());

    HttpMessageConverter<?> converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));

    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    fastConverter.setSupportedMediaTypes(fastMediaTypes);
    List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
    converters.add(1,converter);
    converters.add(fastConverter);
    return restTemplate;
}
 
Example #12
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 #13
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 #14
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 #15
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 #16
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 #17
Source File: WebMVCConfig.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 自定义 一个 json 转换器
 * 详见 fast json 官方文档
 *
 * @return
 */
private HttpMessageConverter getFastJsonConverter() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    //自定义配置...
    com.alibaba.fastjson.support.config.FastJsonConfig config = new com.alibaba.fastjson.support.config.FastJsonConfig();
    SerializerFeature[] features = {
            SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.PrettyFormat};
    config.setSerializerFeatures(features);
    config.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    config.setCharset(StandardCharsets.UTF_8);
    converter.setFastJsonConfig(config);
    return converter;

}
 
Example #18
Source File: WebConfig.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    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);
    return httpMessageConverter;
}
 
Example #19
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 #20
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 #21
Source File: Configs.java    From verifydata with Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverter httpMessageConverter() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    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);
    fastConverter.setSupportedMediaTypes(supportedMediaTypes);
    return fastConverter;
}
 
Example #22
Source File: WebAppConfigurer.java    From spring-cloud-learning with MIT License 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);

    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

    FastJsonConfig fastJsonConfig = new FastJsonConfig();

    // PrettyFormat:结果是否格式化,默认为false
    // WriteMapNullValue:是否输出值为null的字段,默认为false
    // DisableCircularReferenceDetect:消除对同一对象循环引用的问题,默认为false
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.PrettyFormat,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.DisableCircularReferenceDetect
    );
    fastConverter.setFastJsonConfig(fastJsonConfig);

    converters.add(fastConverter);
}
 
Example #23
Source File: WebMvcConfig.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
/**
 * 自定义消息转换器:使用 Fastjson 替换 Spring Boot 默认使用的 Jackson
 *
 * @param converters 消息转换器
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    // 清除默认 Json 转换器
    converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);

    // 配置 FastJson
    FastJsonConfig config = new FastJsonConfig();
    config.setSerializerFeatures(SerializerFeature.QuoteFieldNames, SerializerFeature.WriteEnumUsingToString,
        SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);

    // 添加 FastJsonHttpMessageConverter
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    fastJsonHttpMessageConverter.setFastJsonConfig(config);
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    converters.add(fastJsonHttpMessageConverter);

    // 添加 StringHttpMessageConverter,解决中文乱码问题
    StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
    converters.add(stringHttpMessageConverter);


}
 
Example #24
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 #25
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 #26
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 #27
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 #28
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 #29
Source File: WebMvcConfig.java    From yue-library with Apache License 2.0 6 votes vote down vote up
/**
 * <p>使用FastJson优先于默认的Jackson做json解析
 * <p>https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	// 1. 确认是否启用FastJsonHttpMessageConverter
	if (!webProperties.isEnabledFastJsonHttpMessageConverter()) {
		return;
	}
	
	// 2. 配置FastJsonHttpMessageConverter
	FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
	FastJsonConfig config = new FastJsonConfig();
	config.setDateFormat(JSON.DEFFAULT_DATE_FORMAT);
	config.setSerializerFeatures(
			SerializerFeature.PrettyFormat, 
			SerializerFeature.BrowserCompatible, 
			SerializerFeature.WriteMapNullValue, 
			SerializerFeature.WriteNullBooleanAsFalse,
			SerializerFeature.WriteNullListAsEmpty, 
			SerializerFeature.WriteNullNumberAsZero,
			SerializerFeature.WriteNullStringAsEmpty
	);
	converter.setFastJsonConfig(config);
	converters.add(0, converter);
	
	log.info("【初始化配置-FastJsonHttpMessageConverter】默认配置为false,当前环境为true:使用FastJson优先于默认的Jackson做json解析 ... 已初始化完毕。");
}
 
Example #30
Source File: WebConfig.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig config = new FastJsonConfig();
    config.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonHttpMessageConverter.setFastJsonConfig(config);
    converters.add(fastJsonHttpMessageConverter);
    converters.add(resourceHttpMessageConverter);
    super.configureMessageConverters(converters);
}