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 ...
随机推荐
- git 服务器新建仓库 远程仓库
Git 服务器搭建 上一章节中我们远程仓库使用了 Github,Github 公开的项目是免费的,但是如果你不想让其他人看到你的项目就需要收费. 这时我们就需要自己搭建一台Git服务器作为私有仓库使用 ...
- R中ifelse、which、%in%的用法
R中ifelse.which.%in%的用法 (2014-02-08 13:54:08)标签: 教育 在R学习过程中,遇到了ifelse.which.%in%,下面分别举例,说明他们的用法.1.ife ...
- 解决jar包乱码 in 创新实训 智能自然语言交流系统
今天用eclipse的fat jar插件,打成jar包.之后再命令行运行...程序的功能是切分大的文件...结果是切分的很正确,但是里面的中文都变成了乱码. 最开始以为是在Eclipse中的编码设置有 ...
- 实验一 ASP.NET应用环境配置 总结
通过本次实验我学会了在Windows XP系统中安装IIS服务器,虽然我的电脑是Windows7系统,但是通过虚拟机软件,配置了一台Windows XP系统的虚拟机,在虚拟机内进行各项配置操作.这次实 ...
- JUnit介绍,JUnit是什么?
JUnit是什么? JJUnit是用于编写和运行可重复的自动化测试的开源测试框架, 这样可以保证我们的代码按预期工作.JUnit可广泛用于工业和作为支架(从命令行)或IDE(如Eclipse)内单独的 ...
- 将项目安装到Maven本地资源库
在Maven中,可以使用“mvn install”打包项目,并自动部署到本地资源库,让其他开发人员使用它. mvn install 注意,当“install”在执行阶段,上述所有阶段 “validat ...
- e868. 获取和设置本地外观
By default, Swing uses a cross-platform look and feel called Metal. In most cases, it is more desira ...
- powerDesigner生成Html及Word
转载地址:https://blog.csdn.net/zdp072/article/details/51900794 1:点击Reports 2:点击New Reports 3:定义名字,选择简体中文 ...
- hadoop hdfs 命令
hdfs命令常用操作: hdfs帮助 -help [cmd] 显示命令的帮助信息 [hadoop@hadoop-01 ~]$ hdfs dfs -help ls 递归显示当前目录下的所有文件: [ha ...
- (https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014550004)Topic: Caught java.io.CharConversionException. ERRORCODE=-4220, SQLSTATE=null
270002WDPN 3 Posts 0 people l ...