springboot实现FastJson解析json数据的方法
作者:ZhangJQKb 时间:2023-11-27 22:12:48
最近在研究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这个属性值
来源:http://blog.csdn.net/zhangjq520/article/details/53738736
标签:spring,boot,fastjson
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现俄罗斯方块的源码分享
2023-02-17 04:20:48
![](https://img.aspxhome.com/file/2023/2/69902_0s.png)
Java如何有效避免SQL注入漏洞的方法总结
2021-11-22 07:01:54
Android自定义控件简单实现侧滑菜单效果
2023-10-30 16:56:57
![](https://img.aspxhome.com/file/2023/0/138970_0s.gif)
Java数据结构之稀疏数组的实现与应用
2023-12-04 22:46:41
![](https://img.aspxhome.com/file/2023/5/74775_0s.png)
Netty 拆包沾包问题解决方案详解
2023-09-01 21:58:50
![](https://img.aspxhome.com/file/2023/0/63100_0s.jpg)
在Unity中实现简单的伪时间同步
2023-09-11 03:40:46
深入探究Spring底层核心原理
2023-03-05 08:32:16
Android基于opencv实现多通道分离与合并
2022-05-04 09:49:29
![](https://img.aspxhome.com/file/2023/8/110758_0s.gif)
基于Java实现缓存Cache的深入分析
2023-08-15 20:53:59
详解在spring boot中消息推送系统设计与实现
2023-05-28 04:17:00
![](https://img.aspxhome.com/file/2023/2/74902_0s.jpg)
java简单手写版本实现时间轮算法
2023-01-05 14:50:38
SpringCloud学习笔记之Feign远程调用
2023-08-15 09:57:01
![](https://img.aspxhome.com/file/2023/1/119111_0s.png)
Spring源码之循环依赖之三级缓存详解
2021-11-13 09:31:56
![](https://img.aspxhome.com/file/2023/7/62737_0s.jpg)
深入理解Java IO的flush
2022-01-02 20:05:53
![](https://img.aspxhome.com/file/2023/9/78229_0s.jpg)
IntelliJ IDEA修改新建文件自动生成注释的user名
2021-12-29 04:27:38
![](https://img.aspxhome.com/file/2023/8/69308_0s.png)
Java double转BigDecimal的注意事项说明
2022-12-13 14:44:38
springboot aspect通过@annotation进行拦截的实例代码详解
2023-10-15 20:12:54
![](https://img.aspxhome.com/file/2023/6/113216_0s.png)
spring mvc 组合mybatis框架实例详解
2023-11-28 04:56:04
![](https://img.aspxhome.com/file/2023/2/59752_0s.png)
JAVA中JSONObject对象和Map对象之间的相互转换
2023-07-13 15:04:28
Android仿银行客户签名并且保存签名的截图文件并命名为本地时间
2023-09-19 22:42:48