zoukankan      html  css  js  c++  java
  • springboot集成fastjson

    @Configuration
    @EnableWebMvc
    public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
        /**
         * fastjson configuration
         */
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
            FastJsonConfig config = new FastJsonConfig();
            config.setSerializerFeatures(SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
            config.setSerializeFilters((ValueFilter) (object, name, value) -> {
                // 针对map类型value为null时输出""
                if (Objects.isNull(value)) {
                    return "";
                }
                // 数字类型转字符串
                if (value instanceof Number) {
                    return value.toString();
                }
                return value;
            });
            ArrayList<MediaType> mediaTypes = Lists.newArrayList();
            mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            mediaTypes.add(MediaType.TEXT_HTML);
            mediaTypes.add(MediaType.APPLICATION_JSON);
            mediaTypes.add(MediaType.TEXT_PLAIN);
            converter.setSupportedMediaTypes(mediaTypes);
            converter.setFastJsonConfig(config);
            // 自定义序列化
            config.getSerializeConfig().put(Response.class, new ResponseSerializer(config));
            converters.add(converter);
        }
    }
    

    自定义序列化实现ObjectSerializer 接口

        public static class ResponseSerializer implements ObjectSerializer {
            private final FastJsonConfig fastJsonConfig;
    
            public ResponseSerializer(FastJsonConfig fastJsonConfig) {
                this.fastJsonConfig = fastJsonConfig;
            }
    
            @Override
            public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
                SerializeWriter out = serializer.getWriter();
                Response response = (Response) object;
                out.write(JSON.toJSONString(response.body, fastJsonConfig.getSerializeConfig(), fastJsonConfig.getSerializeFilters(), fastJsonConfig.getSerializerFeatures()));
            }
        }
    

    jsonp注解支持

    fastjson已经内置了一个ResponseBodyAdvice接口的实现JSONPResponseBodyAdvice
    我们只需要配置这个Bean就行了

    @Configuration
    @EnableWebMvc
    public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
        /**
         * use {@link ResponseJSONP}
         * controller return jsonp data
         */
        @Bean
        public ResponseBodyAdvice fastJsonpResponseBodyAdvice() {
            return new JSONPResponseBodyAdvice();
        }
    }
    
  • 相关阅读:
    前端 二
    web 作业 01
    前端 一
    mysql 了解性知识
    数据查询优化之mysql索引
    yml格式或者叫做YAML格式数据学习
    执行sql报错:Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
    大型网站架构之分布式消息队列(转)
    intellj利用 maven自动生成dto,mapper文件
    对memcached使用的总结和使用场景(转)
  • 原文地址:https://www.cnblogs.com/wilwei/p/10244649.html
Copyright © 2011-2022 走看看