码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot整合FastJson

时间:2018-06-30 13:00:27      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:开源   long   mvcc   iba   nts   mvc   new   turn   ali   

fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件。它是目前json解析最快的开源组件没有之一!
我们进行Springboot整合FastJson
一:添加依赖
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.33</version>
        </dependency>

添加依赖我们编写自己的类来继承

@Configuration
public class FastJsonConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = fastJsonHttpMessageConverter.getFastJsonConfig();
        List<MediaType> fastMediaTypes = new ArrayList();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue
        );
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastJsonHttpMessageConverter);
    }
}

三:自己的JSON类

package org.spring.springboot.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;
import java.util.List;

public class MyJson<T> implements Serializable{

        public static final int FAIL = 0;
        public static final int SUCCESS = 1;
        public static final int TOKEN = 2;
        public static final int OTHER = 9;
        private static final long serialVersionUID = -8528102467884030980L;
        private String msg = "success";
        private int code = 1;
        private T data;

    public MyJson(T data) {
        this.data = data;
    }

    public <E> E converDataEntity(Class<E> clazz) {
            return JSON.parseObject(JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteNonStringValueAsString}), clazz);
        }

        public JSONObject converDataJSONObject() {
            return JSON.parseObject(JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteNonStringValueAsString}));
        }

        public <E> List<E> converDataArray(Class<E> clazz) {
            return JSON.parseArray(JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteNonStringValueAsString}), clazz);
        }

        public JSONArray converDataJSONArray() {
            return JSON.parseArray(JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteNonStringValueAsString}));
        }

        public String getMsg() {
            return this.msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public int getCode() {
            return this.code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public T getData() {
            return this.data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }

 

 

Springboot整合FastJson

标签:开源   long   mvcc   iba   nts   mvc   new   turn   ali   

原文地址:https://www.cnblogs.com/bockpecehhe/p/9246481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!