最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot如何使用Fastjson解析Json數(shù)據(jù)

 更新時(shí)間:2020年03月17日 11:56:34   作者:玉天恒  
這篇文章主要介紹了SpringBoot如何使用Fastjson解析Json數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

方法一:

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

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.15</version>
</dependency>

2.修改啟動(dòng)文件

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.修改實(shí)體類

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 {
  
  /**
   * 建立請(qǐng)求映射
   *
   */
  @RequestMapping("/hello")
  public String hello() {
    return "hello";
  }
  
  /**
   * SpringBoot默認(rèn)的解析框架Jackson
   *
   */
  @RequestMapping("/getUser")
  public User gerUser()
  {
    User user = new User();
    user.setId(1);
    user.setUsername("天恒");
    user.setPassword("123456");
    user.setCreateTime(new Date());
    //此信息不會(huì)被返回
    user.setRemark("這是備注信息!");
    return user;
  }
}

5.啟動(dòng)項(xiàng)目,在瀏覽器輸入地址:http://localhost:8080/getUser

方法二:除了啟動(dòng)類,其余代碼都和方法一一樣

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

}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Framework常用面試題及答案匯總

    Spring Framework常用面試題及答案匯總

    這篇文章主要介紹了Spring Framework常用面試題及答案匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java基礎(chǔ)之final關(guān)鍵字作用案例

    Java基礎(chǔ)之final關(guān)鍵字作用案例

    這篇文章主要介紹了Java基礎(chǔ)之final關(guān)鍵字作用案例,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Java 中責(zé)任鏈模式實(shí)現(xiàn)的三種方式

    Java 中責(zé)任鏈模式實(shí)現(xiàn)的三種方式

    本文重點(diǎn)給大家介紹java中如何編寫責(zé)任鏈模式。主要從下面3個(gè)框架中的代碼中介紹。非常不錯(cuò),需要的朋友參考下吧
    2017-09-09
  • Java泛型extends關(guān)鍵字設(shè)置邊界的實(shí)現(xiàn)

    Java泛型extends關(guān)鍵字設(shè)置邊界的實(shí)現(xiàn)

    這篇文章主要介紹了Java泛型extends關(guān)鍵字設(shè)置邊界的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 帶有@Transactional和@Async的循環(huán)依賴問題的解決

    帶有@Transactional和@Async的循環(huán)依賴問題的解決

    這篇文章主要介紹了帶有@Transactional和@Async的循環(huán)依賴問題的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Java基礎(chǔ) Servlet監(jiān)聽器詳解

    Java基礎(chǔ) Servlet監(jiān)聽器詳解

    這篇文章主要介紹了Java基礎(chǔ) Servlet監(jiān)聽器詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 基于Bigdecimal科學(xué)計(jì)數(shù)問題

    基于Bigdecimal科學(xué)計(jì)數(shù)問題

    這篇文章主要介紹了基于Bigdecimal科學(xué)計(jì)數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 解決SpringBoot掃描不到公共類的實(shí)體問題

    解決SpringBoot掃描不到公共類的實(shí)體問題

    這篇文章主要介紹了解決SpringBoot掃描不到公共類的實(shí)體問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java 中繼承和多態(tài)詳細(xì)介紹

    java 中繼承和多態(tài)詳細(xì)介紹

    這篇文章主要介紹了java 中繼承和多態(tài)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • SpringMVC+Mybatis二維碼實(shí)現(xiàn)多平臺(tái)付款(附源碼)

    SpringMVC+Mybatis二維碼實(shí)現(xiàn)多平臺(tái)付款(附源碼)

    本文主要實(shí)現(xiàn)微信支付寶等支付平臺(tái)合多為一的二維碼支付,并且實(shí)現(xiàn)有效時(shí)間內(nèi)支付有效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評(píng)論

白银市| 光泽县| 义马市| 梨树县| 朝阳县| 寿阳县| 台中县| 土默特左旗| 松潘县| 聂拉木县| 千阳县| 三亚市| 宁阳县| 鸡东县| 扶沟县| 如皋市| 佛学| 丰都县| 安达市| 军事| 沅江市| 沈阳市| 藁城市| 江陵县| 嘉义县| 金乡县| 深水埗区| 大新县| 搜索| 内黄县| 绿春县| 神农架林区| 旌德县| 措勤县| 陵川县| 赣州市| 永平县| 海原县| 运城市| 青浦区| 武平县|