Springboot配置Gson

首先除去json包,然后引入Gson的依赖
在这里插入图片描述
然后在配置类中做如下配置:
在这里插入图片描述

package com.config;



import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;


/**
 * @author juge
 * @version 1.0
 * @date 2020/5/30 20:42
 */
@Configuration
public class WebMvcConfig {
    @Bean
    GsonHttpMessageConverter gsonHttpMessageConverter(){
        GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
        GsonBuilder gb = new GsonBuilder();
        Gson gson = gb.setDateFormat("yyyy/MM/dd").create();
        converter.setGson(gson);
        return converter;
    }
}

或者:
在这里插入图片描述

package com.config;



import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;


/**
 * @author juge
 * @version 1.0
 * @date 2020/5/30 20:42
 */
@Configuration
public class WebMvcConfig {

    @Bean
    GsonBuilder gsonBuilder(){
        GsonBuilder gb = new GsonBuilder();
        gb.setDateFormat("yyyy/MM/dd").create();
        return gb;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中集成Gson的过程中,需要注意版本差异。本文所使用的Spring Boot版本为2.3.5.RELEASE。为了集成Gson,首先需要排除原来的Jackson依赖,然后导入Gson依赖。在pom.xml文件中可以进行如下配置: ```xml <!-- Spring Boot web容器 --> dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 排除Jackson依赖 --> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </exclusion> </exclusions> </dependency> <!-- 导入Gson依赖 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> ``` 通过这样的配置,就可以在Spring Boot中成功集成Gson。集成Gson后,可以支持Spring和Spring Boot集成JSON格式数据的序列化和反序列化,同时也支持XML和Protobuf格式的数据序列化和反序列化。此外,可以根据需要随意扩展和替换JSON转换框架,例如Fastjson和Jackson等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Spring Boot整合系列——Gson完整详细版](https://blog.csdn.net/Saykuray/article/details/110506500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [一个简洁的java http框架.rar](https://download.csdn.net/download/Gefangenes/87841515)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值