spring boot之FastJson解析JSON数据

第一步:在pom中引入fastjson依赖:


  com.alibaba
  fastjson
  1.2.15

第二部:配置FastJson(支持两种方法)

第一种方法:

1.启动类继承extends WebMvcConfigurationSupport

2.覆盖方法configureMessageConverters

package com.huawei;

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.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用SpringBootApplication指定一个spring boot的启动类
 * Hello world!
 *
 */
@SpringBootApplication
public class App extends WebMvcConfigurationSupport
{
    /**
     * 在main方法中启动我们的应用程序
     * @param args
     */
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
    @Override
    public void configureMessageConverters(List> converters)
    {
        /**
         * 1.需要先定义一个convert转换消息的对象
         * 2.添加fastjosn的配置信息,比如:是否需要格式化返回的json数据
         * 3.在convert中添加配置信息
         * 4.将convert添加到converters当中
         */
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        /*设置编码格式,解决乱码问题*/
        List fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        converters.add(fastJsonHttpMessageConverter);
    }

}

运行:

spring boot之FastJson解析JSON数据_第1张图片

第二种方法:通过注解@Bean实现

package com.huawei;

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.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用SpringBootApplication指定一个spring boot的启动类
 * Hello world!
 *
 */
@SpringBootApplication
public class App
{
    /**
     * 在main方法中启动我们的应用程序
     * @param args
     */
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverter() {

        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        /*设置编码格式,解决乱码问题*/
        List fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);

        HttpMessageConverter converter = fastJsonHttpMessageConverter;
        return new HttpMessageConverters(converter);
    }

}

运行结果

spring boot之FastJson解析JSON数据_第2张图片

 

你可能感兴趣的:(springBoot学习之路)