springboot整合gson


在打印類的日志的時候,有時候類的屬性很長,打印出來一個很長的信息,不易觀看屬性的內容,來判斷錯誤的內容!但現在我發現了一個工具gson,可以讓你的類打印成為一個json以便觀看!

1.maven導包

<!--json轉換工具-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

2.寫一個工具類:JsonUtil

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
* json的工具
*/
public class JsonUtil {

public static String toJson(Object object){
GsonBuilder gsonBuilder=new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson=gsonBuilder.create();
return gson.toJson(object);
}
}

3.測試工具

package com.xuy.testjson;

import javafx.scene.media.VideoTrack;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class TestJson {

@Test
public void testJson(){
Student xuy = new Student("xuy", 18, true);
log.info("[學生] xuy={}", JsonUtil.toJson(xuy));
}
}


4.觀看打印信息

 

 

這樣就比較便於觀看啦!
————————————————
版權聲明:本文為CSDN博主「阿毅。」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_44265189/article/details/101233360


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM