zoukankan      html  css  js  c++  java
  • springBoot系列教程05:fastjson的集成、配置及使用

    springBoot自带的json用着不太习惯,已习惯了fastJSON,下面介绍下fastjson的配置

    1. pom引入

        <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.15</version>
            </dependency>

    2.配置时间格式及编码,反正中文乱码

    package com.xiao.config;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    
    @Configuration
    public class FastJSONConfig extends WebMvcConfigurerAdapter {
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
            // 处理中文乱码问题
            List<MediaType> fastMediaTypes = new ArrayList<>();
            fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            fastConverter.setSupportedMediaTypes(fastMediaTypes);
            fastConverter.setFastJsonConfig(fastJsonConfig);
            converters.add(fastConverter);
        }
    }

    3.代码测试

    @RequestMapping(value = "/json/test")
        public Result jsonTest(@Valid User user) {
            System.out.println(JSON.toJSONString(user));
            return new Result(user);
        }

    4.结果

  • 相关阅读:
    怎樣在不同DB環境生成其它DB的存儲過程
    XML之sql:column用法对性能影响
    XML之sql:variable性能比较
    环回链接服务器
    动态列名数据视图实现
    一起学习win8应用1构建我们的第一个应用
    linux 限制root SSH登陆和限制su
    nginx 直接在配置文章中设置日志分割
    linux建立ssh信任关系
    linux系统meminfo详解(待补充)
  • 原文地址:https://www.cnblogs.com/xiaochangwei/p/8037400.html
Copyright © 2011-2022 走看看