springboot实现FastJson解析json数据的方法

发布时间 - 2026-01-11 00:53:50    点击率:

最近在研究springboot实现FastJson解析json数据的方法,那么今天也算个学习笔记吧!

添加jar包:

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

两种方式启动加载类:

第一种继承WebMvcConfigurerAdapter,重写configureMessageConverters方法:

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
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; 
 
@SpringBootApplication 
public class App extends WebMvcConfigurerAdapter{ 
public static void main(String[] args) { 
SpringApplication.run(App.class, args); 
} 
 
@Override 
public void configureMessageConverters( 
List<HttpMessageConverter<?>> converters) { 
// TODO Auto-generated method stub 
super.configureMessageConverters(converters); 
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); 
  
    FastJsonConfig fastJsonConfig = new FastJsonConfig(); 
    fastJsonConfig.setSerializerFeatures( 
        SerializerFeature.PrettyFormat 
    ); 
    fastConverter.setFastJsonConfig(fastJsonConfig); 
    
    converters.add(fastConverter); 
}  
 
}  

第二种方式bean注入HttpMessageConverters:

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.autoconfigure.web.HttpMessageConverters; 
import org.springframework.context.annotation.Bean; 
import org.springframework.http.converter.HttpMessageConverter; 
import com.alibaba.fastjson.serializer.SerializerFeature; 
import com.alibaba.fastjson.support.config.FastJsonConfig; 
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 
 
@SpringBootApplication 
public class AppTwo{ 
 
public static void main(String[] args) { 
SpringApplication.run(AppTwo.class, args); 
} 
 
@Bean 
public HttpMessageConverters fastJsonHttpMessageConverters() { 
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); 
FastJsonConfig fastJsonConfig = new FastJsonConfig(); 
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); 
fastConverter.setFastJsonConfig(fastJsonConfig); 
HttpMessageConverter<?> converter = fastConverter; 
return new HttpMessageConverters(converter); 
} 
 
} 

最后属性前加@JSONField:

@JSONField(serialize=false) 
private Long id; 

返回前端就会没有id这个属性值

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# spring  # boot  # fastjson  # 解析json  # springboot  # fastjsong  # springboot中使用FastJson解决long类型在js中失去精度的问题  # SpringBoot整合Gson 整合Fastjson的实例详解  # SpringBoot如何使用Fastjson解析Json数据  # springboot中用fastjson处理返回值为null的属性值  # 使用SpringBoot+OkHttp+fastjson实现Github的OAuth第三方登录  # SpringBoot整合FastJson过程解析  # SpringBoot Redis配置Fastjson进行序列化和反序列化实现  # Spring Boot使用FastJson解析JSON数据的方法  # Spring boot详解fastjson过滤字段为null值如何解决  # 就会  # 两种  # 重写  # 也算  # 第二种  # 第一种  # 大家多多  # 学习笔记  # 加载  # import  # java  # org  # configureMessageConverters  # serializer  # autoconfigure  # annotation  # WebMvcConfigurerAdapter  # springframework  # HttpMessageConverter  # converter 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 制作企业网站建设方案,怎样建设一个公司网站?  Swift开发中switch语句值绑定模式  JS经典正则表达式笔试题汇总  Laravel Octane如何提升性能_使用Laravel Octane加速你的应用  html文件怎么打开证书错误_https协议的html打开提示不安全【指南】  JS去除重复并统计数量的实现方法  Laravel如何使用Blade模板引擎?(完整语法和示例)  使用PHP下载CSS文件中的所有图片【几行代码即可实现】  Laravel如何自定义分页视图?(Pagination示例)  高防服务器:AI智能防御DDoS攻击与数据安全保障  实现点击下箭头变上箭头来回切换的两种方法【推荐】  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  Internet Explorer官网直接进入 IE浏览器在线体验版网址  如何确保西部建站助手FTP传输的安全性?  如何快速搭建高效简练网站?  HTML透明颜色代码怎么让下拉菜单透明_下拉菜单透明背景指南【技巧】  laravel怎么使用数据库工厂(Factory)生成带有关联模型的数据_laravel Factory生成关联数据方法  如何在阿里云ECS服务器部署织梦CMS网站?  Laravel的.env文件有什么用_Laravel环境变量配置与管理详解  如何在新浪SAE免费搭建个人博客?  Laravel怎么在Controller之外的地方验证数据  如何用花生壳三步快速搭建专属网站?  高端建站如何打造兼具美学与转化的品牌官网?  iOS验证手机号的正则表达式  HTML5建模怎么导出为FBX格式_FBX格式兼容性及导出步骤【指南】  html5如何设置样式_HTML5样式设置方法与CSS应用技巧【教程】  Android实现代码画虚线边框背景效果  Laravel怎么上传文件_Laravel图片上传及存储配置  Android使用GridView实现日历的简单功能  android nfc常用标签读取总结  Laravel的路由模型绑定怎么用_Laravel Route Model Binding简化控制器逻辑  轻松掌握MySQL函数中的last_insert_id()  如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?  php在windows下怎么调试_phpwindows环境调试操作说明【操作】  JS碰撞运动实现方法详解  Laravel事件和监听器如何实现_Laravel Events & Listeners解耦应用的实战教程  详解Android中Activity的四大启动模式实验简述  利用 Google AI 进行 YouTube 视频 SEO 描述优化  非常酷的网站设计制作软件,酷培ai教育官方网站?  如何彻底卸载建站之星软件?  Laravel如何实现多对多模型关联?(Eloquent教程)  如何快速上传建站程序避免常见错误?  Laravel项目结构怎么组织_大型Laravel应用的最佳目录结构实践  网站制作大概多少钱一个,做一个平台网站大概多少钱?  谷歌Google入口永久地址_Google搜索引擎官网首页永久入口  Laravel如何使用Telescope进行调试?(安装和使用教程)  Laravel Livewire是什么_使用Laravel Livewire构建动态前端界面  Laravel Eloquent模型如何创建_Laravel ORM基础之Model创建与使用教程  详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)  Laravel安装步骤详细教程_Laravel环境搭建指南