91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot如何使用Fastjson解析Json數據

發布時間:2020-09-18 22:22:48 來源:腳本之家 閱讀:234 作者:玉天恒 欄目:編程語言

方法一:

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

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

都昌县| 斗六市| 台北市| 喀喇沁旗| 栾川县| 辽中县| 嵊州市| 马鞍山市| 会东县| 六盘水市| 千阳县| 小金县| 波密县| 宾阳县| 太康县| 三明市| 长宁区| 吉林省| 临夏县| 鹤峰县| 卢龙县| 榕江县| 冷水江市| 唐山市| 衡山县| 稷山县| 田东县| 临洮县| 渭源县| 五台县| 开鲁县| 龙泉市| 英山县| 宜章县| 淮阳县| 宿松县| 嘉兴市| 龙井市| 八宿县| 宣恩县| 民县|