方法一:

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数据的更多相关文章

  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. 接口与virtual,override,new关键字

    一,类继承接口 1,首先我们定义一个简单的ITeacher接口,并定义一个Professor类继承它. public interface ITeacher { void Print(); } publ ...

  2. hbase源码系列(九)StoreFile存储格式

    从这一章开始要讲Region Server这块的了,但是在讲Region Server这块之前得讲一下StoreFile,否则后面的不好讲下去,这块是基础,Region Sever上面的操作,大部分都 ...

  3. js 的数值限制可能引起的问题

    源于:https://raw.github.com/ruanyf/jstutorial/gh-pages/grammar/number.md 1. 根据国际标准IEEE 754,64位浮点数格式的64 ...

  4. native-base中icon不能正确显示[转]

    初次接触native-base,在使用它的Icon组件的时候碰到了一个问题:图标没能正确显示!(在expo调试模式下是正常的) native-base官网给的使用Icon的例子 怎么找到适合我的图标呢 ...

  5. 巧用style的另类写法

    看到style,不少人可能会说这个我知道,就是控件写属性的话可以通过style来实现代码的复用,单独把这些属性及其参数写成style就可以便捷的调用. <?xml version="1 ...

  6. HBase什么时候作minor major compact

    HBase什么时候做minor major compact我们都知道compact分为两类,一类叫Minor compact ,一类叫Major compact,两者有什么区别呢?两者的区别在于:Mi ...

  7. Mac 系统上安装Lua和SubmlimeText 编译器

    第一步:安装命令 curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz tar zxf lua-5.2.3.tar.gz cd lua-5.2.3 ma ...

  8. Numpy 用于数组的文件输入和输出

    将数组以二进制格式保存 np.save 和np.load 是读写磁盘数组数据的两个主要函数.默认情况下,数组是以未压缩的原始二进制格式进行保持在扩展名 为.npy的文件中的 如果文件路径末尾没有扩展名 ...

  9. python-切片实例

    针对list或tuple取指定范围的操作.可以使用切片(slice),非常有用 1.list:可变数组 L=['a','b','c','d','e'] >>> L[0:3] #从第0 ...

  10. MFC绘图小实验(2)

    1,以正五边形的5个顶点为基础,隔点存储构成五角星.填充模式采用WINDING.五角星边界线为5个像素宽的蓝色实线,内部使用红色填充. CRect rect; //定义矩形 GetClientRect ...