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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

ZhangJQKb   2021-03-24 我要评论
想了解springboot实现FastJson解析json数据的方法的相关内容吗,ZhangJQKb在本文为您仔细讲解spring boot fastjson的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,boot,fastjson,spring,boot,解析json,springboot,fastjsong,下面大家一起来学习吧。

最近在研究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这个属性值

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们