方法一:

1.在pom.xml文件下添加依赖包

  1. <dependency>
  2.   <groupId>com.alibaba</groupId>
  3.   <artifactId>fastjson</artifactId>
  4.   <version>1.2.15</version>
  5. </dependency>

2.修改启动文件

  1. package myshop;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.http.converter.HttpMessageConverter;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  9.  
  10. import com.alibaba.fastjson.serializer.SerializerFeature;
  11. import com.alibaba.fastjson.support.config.FastJsonConfig;
  12. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  13.  
  14. @SpringBootApplication
  15. public class App extends WebMvcConfigurerAdapter{
  16. @Override
  17. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  18. // TODO Auto-generated method stub
  19. super.configureMessageConverters(converters);
  20.  
  21. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  22. FastJsonConfig fastConfig = new FastJsonConfig();
  23. fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
  24. fastConverter.setFastJsonConfig(fastConfig);
  25. converters.add(fastConverter);
  26. }
  27.  
  28. public static void main(String[] args) {
  29. // TODO Auto-generated method stub
  30. SpringApplication.run(App.class, args);
  31. }
  32.  
  33. }

3.修改实体类

  1. package myshop.entity;
  2.  
  3. import java.util.Date;
  4.  
  5. import com.alibaba.fastjson.annotation.JSONField;
  6.  
  7. /**
  8. * 用户类
  9. *
  10. */
  11. public class User {
  12. private int id;
  13. private String username;
  14. private String password;
  15. @JSONField(format = "yyyy-MM-dd HH-mm")
  16. private Date createTime;
  17. /**
  18. * 如果不希望返回remark信息
  19. * serialize是否序列化
  20. */
  21. @JSONField(serialize = false)
  22. private String remark;
  23. public int getId() {
  24. return id;
  25. }
  26. public void setId(int id) {
  27. this.id = id;
  28. }
  29. public String getUsername() {
  30. return username;
  31. }
  32. public void setUsername(String username) {
  33. this.username = username;
  34. }
  35. public String getPassword() {
  36. return password;
  37. }
  38. public void setPassword(String password) {
  39. this.password = password;
  40. }
  41. public Date getCreateTime() {
  42. return createTime;
  43. }
  44. public void setCreateTime(Date createTime) {
  45. this.createTime = createTime;
  46. }
  47. public String getRemark() {
  48. return remark;
  49. }
  50. public void setRemark(String remark) {
  51. this.remark = remark;
  52. }
  53. }

4.修改控制器

  1. package myshop.controller;
  2.  
  3. import java.util.Date;
  4.  
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7.  
  8. import myshop.entity.User;
  9.  
  10. /**
  11. * @RestController = @Controller + @RequestBody
  12. *
  13. */
  14. @RestController
  15. public class HelloController {
  16.  
  17. /**
  18. * 建立请求映射
  19. *
  20. */
  21. @RequestMapping("/hello")
  22. public String hello() {
  23. return "hello";
  24. }
  25.  
  26. /**
  27. * SpringBoot默认的解析框架Jackson
  28. *
  29. */
  30. @RequestMapping("/getUser")
  31. public User gerUser()
  32. {
  33. User user = new User();
  34. user.setId(1);
  35. user.setUsername("天恒");
  36. user.setPassword("123456");
  37. user.setCreateTime(new Date());
  38. //此信息不会被返回
  39. user.setRemark("这是备注信息!");
  40. return user;
  41. }
  42. }

5.启动项目,在浏览器输入地址:http://localhost:8080/getUser

方法二:除了启动类,其余代码都和方法一一样

  1. package myshop;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.http.converter.HttpMessageConverter;
  8.  
  9. import com.alibaba.fastjson.serializer.SerializerFeature;
  10. import com.alibaba.fastjson.support.config.FastJsonConfig;
  11. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  12.  
  13. @SpringBootApplication
  14. public class App {
  15.  
  16. @Bean
  17. public HttpMessageConverters fastJsonHttpMessageConverter()
  18. {
  19. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  20. FastJsonConfig fastConfig = new FastJsonConfig();
  21. fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
  22. fastConverter.setFastJsonConfig(fastConfig);
  23.  
  24. HttpMessageConverter<?> converts = fastConverter;
  25. return new HttpMessageConverters(converts);
  26. }
  27.  
  28. public static void main(String[] args) {
  29. // TODO Auto-generated method stub
  30. SpringApplication.run(App.class, args);
  31. }
  32.  
  33. }

SpringBoot------使用Fastjson解析Json数据的更多相关文章

  1. Spring Boot返回json数据及完美使用FastJson解析Json数据

     Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...

  2. spring boot (二):使用fastJson解析json数据

    如果我们想在spring boot中使用第三方的json解析框架: 1)我们需要在pom.xml文件中引入第三方包的依赖; 2)实现方法: 方法1 需要在启动类中继承WebMvcConfigurerA ...

  3. 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...

  4. 66、fastJson 解析json数据时,如果key值不同怎么处理?

    在某些场景,你可能需要定制序列化输出,比如说,希望序列化采用之后采用"ID",而不是"id",你可以使用@JSONField这个Annotation. publ ...

  5. springboot使用fastJson作为json解析框架

    springboot使用fastJson作为json解析框架 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 〇.搭建spri ...

  6. fastjson生成和解析json数据,序列化和反序列化数据

    本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...

  7. fastjson生成和解析json数据

    本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...

  8. java分享第十三天(fastjson生成和解析json数据,序列化和反序列化数据)

     fastjson简介:Fastjson是一个Java语言编写的高性能功能完善的JSON库.fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jack ...

  9. Android网络之数据解析----使用Google Gson解析Json数据

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

随机推荐

  1. Spark Shuffle原理解析

    Spark Shuffle原理解析 一:到底什么是Shuffle? Shuffle中文翻译为“洗牌”,需要Shuffle的关键性原因是某种具有共同特征的数据需要最终汇聚到一个计算节点上进行计算. 二: ...

  2. 通过github搭建个人博客

    今天突发奇想,想用GitHub搭建一个个人博客,就大概学习了一下,特此记录. 其实非常简单,首先要知道,这里是通过GitHub Pages进行搭建的,什么?不知道什么是GitHub Pages?Git ...

  3. Python基础知识小结

    1. 关于函数传参 def func(n, *args, **kwargs): print n print args print kwargs if __name__ == '__main__': # ...

  4. IEnumerable<T> 转换为数组

    IEnumerable<User> userlist=xxxx; string[] ids=userlist.select(u=>u.id).toArray();

  5. SERVER2012 FTP服务器和客户端配置

    SERVER2012 用IIS8 搭建的FTP 默认是主动模式的,导致花了一些时间去研究如何连接,总结了一下配置,希望能帮到需要的同学,以下介绍下步骤 1.服务器--计算机管理-用户-新建用户--默认 ...

  6. maven 从私仓库下载jar包

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  7. jQuery imgAreaSelect Examples

    案例:前端图片截取功能 分布说明A:选择File本地选择的图片 B:根据需求按比例缩放图片 C:区域选择型操作 A: 选择图片 <input class="upfile" t ...

  8. Genymotion 解决虚拟镜像下载速度特别慢的问题[转]

    Genymotion下载地址(需注册账号):https://www.genymotion.com/download/ Genymotion号称Android模拟器中运行最快的,但是服务器在国外,And ...

  9. 漂亮的圆角文本框 CSS3实现

    进入人人小站发现了个挺好看的文本框,圆角的,原来是利用了CSS3的效果,不过暂时只有IE9 和FF浏览器支持. <html xmlns="http://www.w3.org/1999/ ...

  10. 高斯分布与Gamma分布关系

    https://math.stackexchange.com/questions/1917647/proving-ex4-3%CF%834