• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot返回多种格式的数据的实现示例

springboot 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论
文章目录[隐藏]

一、SpringBoot整合FastJson

1.1、引入FastJson依赖包

maven项目:

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

gradle项目:

compile 'com.alibaba:fastjson:1.2.78'    // 引入fastjson

1.2、创建一个Web MVC的配置类,并放在springboot扫描包路径下。

package com.it.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

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

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 1.springboot默认使用Jaskson组件,需要先移除Jaskson组件
        for (HttpMessageConverter<?> converter : converters) { // 循环所有的转换器
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                converters.remove(converter); // 删除Jaskson转换器
            }
        }
        // 2. 项目中添加fastJson转换器
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        // 3. 配置fastJson转换器
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures( // 配置序列化相关操作
                SerializerFeature.WriteMapNullValue,              // 允许Map内容为null
                SerializerFeature.WriteNullListAsEmpty,           // list集合为null使用[]代替
                SerializerFeature.WriteNullStringAsEmpty,         // String内容为null使用空文字代替
                SerializerFeature.WriteDateUseDateFormat,         // 日期格式化输出
                SerializerFeature.WriteNullNumberAsZero,          // 数字为空使用0代替
                SerializerFeature.DisableCircularReferenceDetect  // 禁用循环引用
        );
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); // 配置fastjson转换处理
        // 4. 配置响应的头信息
        List<MediaType> fastJsonMediaTypes = new ArrayList<>(); // 所有的响应类型
        fastJsonMediaTypes.add(MediaType.APPLICATION_JSON); // 使用JSON类型进行相应
        fastJsonHttp<i style="color:transparent">本文来源gaodai$ma#com搞$代*码6网</i>MessageConverter.setSupportedMediaTypes(fastJsonMediaTypes);
        // 5. 转换器列表中添加配置好的fastjson组件
        converters.add(fastJsonHttpMessageConverter);
    }
}

1.3、测试fastjson是否引入成功。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot返回多种格式的数据的实现示例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址