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.结果

  • 相关阅读:
    MySQL 8.0系列——轻松改配置,云上友好
    测试expire_logs_days参数
    mongodb单实例安装
    搭建PXC集群指引
    控制mysqldump导出的SQL文件的事务大小
    实战MySQL8.0.17 Clone Plugin
    windows环境下 curl 安装和使用
    git 创建tag , 查看tag , 删除tag
    git 基本操作
    git 一个分支完全覆盖另一个分支
  • 原文地址:https://www.cnblogs.com/xiaochangwei/p/8037400.html
Copyright © 2011-2022 走看看