美文网首页springboot后台
Spring Boot集成fastjson

Spring Boot集成fastjson

作者: 问题_解决_分享_讨论_最优 | 来源:发表于2020-01-05 01:05 被阅读0次

    一、项目准备

    直接使用上个章节的源码,Spring Boot集成jsp

    二、添加依赖

    spring-boot-starter-web里去除spring-boot-starter-json依赖,添加fastjson依赖:

    <!-- Spring Boot web启动器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-json</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
    

    三、配置fastjson

    方法一:注入bean方式

    在启动类Application里添加public HttpMessageConverters fastJsonHttpMessageConverters()方法:

    package com.songguoliang.springboot;
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
    import org.springframework.context.annotation.Bean;
    import org.springframework.http.converter.HttpMessageConverter;
    import tk.mybatis.spring.annotation.MapperScan;
    
    /**
     * @Description
     * @Author sgl
     * @Date 2018-05-02 14:51
     */
    @SpringBootApplication
    @MapperScan("com.songguoliang.springboot.mapper")
    public class Application{
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
        /**
         * 覆盖方法configureMessageConverters,使用fastJson
         * @return
         */
        @Bean
        public HttpMessageConverters fastJsonHttpMessageConverters() {
            //1、定义一个convert转换消息的对象
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
            //2、添加fastjson的配置信息
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            //3、在convert中添加配置信息
            fastConverter.setFastJsonConfig(fastJsonConfig);
            //4、将convert添加到converters中
            HttpMessageConverter<?> converter = fastConverter;
            return new HttpMessageConverters(converter);
        }
    
    }
    
    方法二:重写configureMessageConverters()方法的方式

    启动类实现org.springframework.web.servlet.config.annotation.WebMvcConfigurer接口
    重写public void configureMessageConverters(List<HttpMessageConverter<?>> converters)方法。

    package com.songguoliang.springboot;
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import tk.mybatis.spring.annotation.MapperScan;
    
    import java.util.List;
    
    /**
     * @Description
     * @Author sgl
     * @Date 2018-05-02 14:51
     */
    @SpringBootApplication
    @MapperScan("com.songguoliang.springboot.mapper")
    public class Application implements WebMvcConfigurer {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            //1、定义一个convert转换消息的对象
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
            //2、添加fastjson的配置信息
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            //3、在convert中添加配置信息
            fastConverter.setFastJsonConfig(fastJsonConfig);
            //4、将convert添加到converters中
            converters.add(fastConverter);
        }
    }
    

    四、创建测试对象

    创建一个model包,然后在该包里创建一个FastjsonTest实体,这个实体里,通过@JSONField注解,格式化date字段的值,并且不把ignore字段返回到前台,内容如下:

    package com.songguoliang.springboot.model;
    
    import com.alibaba.fastjson.annotation.JSONField;
    
    import java.util.Date;
    
    /**
     * @Description
     * @Author sgl
     * @Date 2018-05-08 13:50
     */
    public class FastjsonTest {
        private Integer id;
        private String string;
        /**
         * 格式化日期
         */
        @JSONField(format = "yyyy-MM-dd HH:mm:ss")
        private Date date;
    
        /**
         * 转换为json时不包括该属性
         */
        @JSONField(serialize = false)
        private String ignore;
    
        //省略getter、setter
    }
    

    五、创建Controller

    创建一个TestController,内容如下:

    package com.songguoliang.springboot.controller;
    
    import com.songguoliang.springboot.model.FastjsonTest;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Date;
    
    /**
     * @Description
     * @Author sgl
     * @Date 2018-05-08 13:55
     */
    @RestController
    public class TestController {
    
        @GetMapping("/fastjson")
        public FastjsonTest getFastJson() {
            FastjsonTest fastjsonTest = new FastjsonTest();
            fastjsonTest.setId(1);
            fastjsonTest.setString("fastjson test");
            fastjsonTest.setIgnore("ignore field");
            fastjsonTest.setDate(new Date());
            return fastjsonTest;
        }
    }
    

    六、启动服务查看测试结果

    启动结果,在浏览器上输入:http://localhost:8080/fastjson,结果如下:

    这里写图片描述

    可以看到,date字段值的格式是我们设置好的yyyy-MM-dd HH:mm:ss,而且ignore字段没有显示出来。

    打个广告,本人博客地址是:风吟个人博客

    相关文章

      网友评论

        本文标题:Spring Boot集成fastjson

        本文链接:https://www.haomeiwen.com/subject/xgvpoctx.html