全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

最近在研究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 


相关文章: ,怎么用自己头像做动态表情包?  洛阳网站制作公司有哪些,洛阳的招聘网站都有哪些?  网站设计制作公司地址,网站建设比较好的公司都有哪些?  西安大型网站制作公司,西安招聘网站最好的是哪个?  jQuery 常见小例汇总  如何在IIS中新建站点并解决端口绑定冲突?  美食网站链接制作教程视频,哪个教做美食的网站比较专业点?  如何通过二级域名建站提升品牌影响力?  家庭建站与云服务器建站,如何选择更优?  青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?  网站制作的方法有哪些,如何将自己制作的网站发布到网上?  建站三合一如何选?哪家性价比更高?  如何在建站之星网店版论坛获取技术支持?  建站之星如何保障用户数据免受黑客入侵?  如何在景安云服务器上绑定域名并配置虚拟主机?  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  行程制作网站有哪些,第三方机票电子行程单怎么开?  建站主机空间推荐 高性价比配置与快速部署方案解析  *服务器网站为何频现安全漏洞?  如何在Golang中实现微服务服务拆分_Golang微服务拆分与接口管理方法  网站制作免费,什么网站能看正片电影?  建站之星3.0如何解决常见操作问题?  北京专业网站制作设计师招聘,北京白云观官方网站?  已有域名能否直接搭建网站?  建站之星如何快速解决建站难题?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?  ,制作一个手机app网站要多少钱?  如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?  建站之星导航配置指南:自助建站与SEO优化全解析  如何通过远程VPS快速搭建个人网站?  c++怎么用jemalloc c++替换默认内存分配器【性能】  建站主机核心功能解析:服务器选择与网站搭建流程指南  如何生成腾讯云建站专用兑换码?  如何在宝塔面板创建新站点?  建站之家VIP精选网站模板与SEO优化教程整合指南  建站之星如何开启自定义404页面避免用户流失?  建站之星安装后界面空白如何解决?  娃派WAP自助建站:免费模板+移动优化,快速打造专业网站  如何彻底删除建站之星生成的Banner?  JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)  nginx修改上传文件大小限制的方法  如何彻底卸载建站之星软件?  如何通过IIS搭建网站并配置访问权限?  网站制作中优化长尾关键字挖掘的技巧,建一个视频网站需要多少钱?  制作表格网站有哪些,线上表格怎么弄?  西安专业网站制作公司有哪些,陕西省建行官方网站?  如何快速搭建高效WAP手机网站吸引移动用户?  如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法  用v-html解决Vue.js渲染中html标签不被解析的问题  国美网站制作流程,国美电器蒸汽鍋怎么用官方网站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。