SpringBoot------使用Fastjson解析Json数据
方法一:
1.在pom.xml文件下添加依赖包
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.15</version>
- </dependency>
2.修改启动文件
- package myshop;
- import java.util.List;
- 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{
- @Override
- public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
- // TODO Auto-generated method stub
- super.configureMessageConverters(converters);
- FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
- FastJsonConfig fastConfig = new FastJsonConfig();
- fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
- fastConverter.setFastJsonConfig(fastConfig);
- converters.add(fastConverter);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SpringApplication.run(App.class, args);
- }
- }
3.修改实体类
- package myshop.entity;
- import java.util.Date;
- import com.alibaba.fastjson.annotation.JSONField;
- /**
- * 用户类
- *
- */
- public class User {
- private int id;
- private String username;
- private String password;
- @JSONField(format = "yyyy-MM-dd HH-mm")
- private Date createTime;
- /**
- * 如果不希望返回remark信息
- * serialize是否序列化
- */
- @JSONField(serialize = false)
- private String remark;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Date getCreateTime() {
- return createTime;
- }
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
- public String getRemark() {
- return remark;
- }
- public void setRemark(String remark) {
- this.remark = remark;
- }
- }
4.修改控制器
- package myshop.controller;
- import java.util.Date;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import myshop.entity.User;
- /**
- * @RestController = @Controller + @RequestBody
- *
- */
- @RestController
- public class HelloController {
- /**
- * 建立请求映射
- *
- */
- @RequestMapping("/hello")
- public String hello() {
- return "hello";
- }
- /**
- * SpringBoot默认的解析框架Jackson
- *
- */
- @RequestMapping("/getUser")
- public User gerUser()
- {
- User user = new User();
- user.setId(1);
- user.setUsername("天恒");
- user.setPassword("123456");
- user.setCreateTime(new Date());
- //此信息不会被返回
- user.setRemark("这是备注信息!");
- return user;
- }
- }
5.启动项目,在浏览器输入地址:http://localhost:8080/getUser
方法二:除了启动类,其余代码都和方法一一样
- package myshop;
- 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 App {
- @Bean
- public HttpMessageConverters fastJsonHttpMessageConverter()
- {
- FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
- FastJsonConfig fastConfig = new FastJsonConfig();
- fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
- fastConverter.setFastJsonConfig(fastConfig);
- HttpMessageConverter<?> converts = fastConverter;
- return new HttpMessageConverters(converts);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SpringApplication.run(App.class, args);
- }
- }
SpringBoot------使用Fastjson解析Json数据的更多相关文章
- Spring Boot返回json数据及完美使用FastJson解析Json数据
Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...
- spring boot (二):使用fastJson解析json数据
如果我们想在spring boot中使用第三方的json解析框架: 1)我们需要在pom.xml文件中引入第三方包的依赖; 2)实现方法: 方法1 需要在启动类中继承WebMvcConfigurerA ...
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
[原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...
- 66、fastJson 解析json数据时,如果key值不同怎么处理?
在某些场景,你可能需要定制序列化输出,比如说,希望序列化采用之后采用"ID",而不是"id",你可以使用@JSONField这个Annotation. publ ...
- springboot使用fastJson作为json解析框架
springboot使用fastJson作为json解析框架 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 〇.搭建spri ...
- fastjson生成和解析json数据,序列化和反序列化数据
本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...
- fastjson生成和解析json数据
本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...
- java分享第十三天(fastjson生成和解析json数据,序列化和反序列化数据)
fastjson简介:Fastjson是一个Java语言编写的高性能功能完善的JSON库.fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jack ...
- Android网络之数据解析----使用Google Gson解析Json数据
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
随机推荐
- Spark Shuffle原理解析
Spark Shuffle原理解析 一:到底什么是Shuffle? Shuffle中文翻译为“洗牌”,需要Shuffle的关键性原因是某种具有共同特征的数据需要最终汇聚到一个计算节点上进行计算. 二: ...
- 通过github搭建个人博客
今天突发奇想,想用GitHub搭建一个个人博客,就大概学习了一下,特此记录. 其实非常简单,首先要知道,这里是通过GitHub Pages进行搭建的,什么?不知道什么是GitHub Pages?Git ...
- Python基础知识小结
1. 关于函数传参 def func(n, *args, **kwargs): print n print args print kwargs if __name__ == '__main__': # ...
- IEnumerable<T> 转换为数组
IEnumerable<User> userlist=xxxx; string[] ids=userlist.select(u=>u.id).toArray();
- SERVER2012 FTP服务器和客户端配置
SERVER2012 用IIS8 搭建的FTP 默认是主动模式的,导致花了一些时间去研究如何连接,总结了一下配置,希望能帮到需要的同学,以下介绍下步骤 1.服务器--计算机管理-用户-新建用户--默认 ...
- maven 从私仓库下载jar包
pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- jQuery imgAreaSelect Examples
案例:前端图片截取功能 分布说明A:选择File本地选择的图片 B:根据需求按比例缩放图片 C:区域选择型操作 A: 选择图片 <input class="upfile" t ...
- Genymotion 解决虚拟镜像下载速度特别慢的问题[转]
Genymotion下载地址(需注册账号):https://www.genymotion.com/download/ Genymotion号称Android模拟器中运行最快的,但是服务器在国外,And ...
- 漂亮的圆角文本框 CSS3实现
进入人人小站发现了个挺好看的文本框,圆角的,原来是利用了CSS3的效果,不过暂时只有IE9 和FF浏览器支持. <html xmlns="http://www.w3.org/1999/ ...
- 高斯分布与Gamma分布关系
https://math.stackexchange.com/questions/1917647/proving-ex4-3%CF%834