SpringBoot如何使用Fastjson解析Json数据

发布时间:2020-09-18 22:22:48 作者:玉天恒
来源:脚本之家 阅读:231

方法一:

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

SpringBoot如何使用Fastjson解析Json数据

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

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);
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. 使用google的Gson库和alibaba的Fastjson库解析json数据的区别
  2. 使用JavaScript怎么解析JSON数据

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot fast

上一篇:对python中dict和json的区别详解

下一篇:Android_RecyclerView实现上下滚动广告条实例(带图片)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》