【Spring Boot】025
【Spring Boot】025
【Spring Boot】025-返回 JSO 数据:常用的三种 JSO 转换器一、第一种:默认的 jackson-databind1、说明默认情况下,类上使用 @Controller 注解,方法上使用 @RespeBody 注解,返回的对象会默认被转换成 JSO 格式;另外,@RestController 注解是 @Controller 注解 和 @
【Spring Boot】025
1、说明
默认情况下,类上使用 @Controller 注解,方法上使用 @RespeBody 注解,返回的对象会默认被转换成 JSO 格式;
另外,@RestController 注解是 @Controller 注解 和 @RespeBody 注解的组合,是等效的!
1、Gson简介
GSO 是 Google 提供的用来在 Java 对象和 JSO 数据之间进行映射的 Java 类库。可以将一个JSO 字符转成一个 Java 对象,或者将一个 Java对象 转化为 JSO 字符串。
特点: 快速、高效;代码量少、简洁;面向对象;数据传递和解析方便
2、使用步骤
第一步:在 pom.xml 中移除默认的 jackson-databind ,引入 Gson 依赖
代码语言:javascript代码运行次数:0运行复制<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusi>
<exclusion>
<groupId>com.fasterxml.</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusi>
</dependency>
<!--gson-->
<dependency>
<groupId>com.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
第二步:自定义 HttpMessageConverter
代码语言:javascript代码运行次数:0运行复制Spring Boot 已经默认提供了 Gson 的自动转换类 GsonHttpMessageConvertersConfigurati,因此 Gson 的以来添加之后,可以直接像使用 jackson-databind 那样直接使用 gson 。但是在 Gson 进行转换时,如果相对日期进行格式化,那么需要开发者自定义 HttpMessageConverter !
package com.zibo.;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.annotation.Bean;
import org.annotation.Configuration;
import org.springframework.json.GsonHttpMessageConverter;
import java.lang.reflect.Modifier;
@Configuration
public class GSOConfig {
@Bean
GsonHttpMessageConverter gsonHttpMessageConverter(){
// 自己提供一个 GSOHttpMessageConverter 实例
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
GsonBuilder builder = new GsonBuilder();
// 设置解析式日期的格式
builder.setDateFormat("yyyy-MM-dd");
// 设置解析时修饰符为 protected 的字段被过滤掉
(Modifier.PROTECTED);
// 创建 Gson 对象,放入 converter 实例并返回
Gson gson = ();
converter.setGson(gson);
return converter;
}
}
1、简介
fastjson 是阿里巴巴的一个开源的 JSO 解析框架,是目前 JSO 解析速度最快的开源框架,该框架也可以集成到 Spring Boot 中,大但并不能立即使用!需要提供相应的 HttpMessageConverter 后才能使用!
2、使用步骤
第一步:在 pom.xml 中移除默认的 jackson-databind ,引入 fastjson 依赖
代码语言:javascript代码运行次数:0运行复制<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusi>
<exclusion>
<groupId>com.fasterxml.</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusi>
</dependency>
<!--fast json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
第二步:自定义 HttpMessageConverter
代码语言:javascript代码运行次数:0运行复制package com.zibo.;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.annotation.Bean;
import org.annotation.Configuration;
import StandardCharsets;
@Configuration
public class MyFastJsonConfig {
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
// 自己提供一个 FastJsonHttpMessageConverter 实例
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
// 设置解析式日期的格式
config.setDateFormat("yyyy-MM-dd");
// 设置解析时编码格式为 UTF-8
config.setCharset(StandardCharsets.UTF_8);
config.setSerializerFeatures(
SerializerFeature.WriteClassame,
SerializerFeature.WriteMapullValue,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteullListAsEmpty,
SerializerFeature.WriteullStringAsEmpty
);
converter.setFastJsonConfig(config);
return converter;
}
}
、另一种配置方式
代码语言:javascript代码运行次数:0运行复制package com.zibo.;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.HttpMessageConverter;
import org.springframework.web.annotation.WebMvcConfigurer;
import StandardCharsets;
import java.util.List;
public class MyWebMvcConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 自己提供一个 FastJsonHttpMessageConverter 实例
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
// 设置解析式日期的格式
config.setDateFormat("yyyy-MM-dd");
// 设置解析时编码格式为 UTF-8
config.setCharset(StandardCharsets.UTF_8);
config.setSerializerFeatures(
SerializerFeature.WriteClassame,
SerializerFeature.WriteMapullValue,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteullListAsEmpty,
SerializerFeature.WriteullStringAsEmpty
);
converter.setFastJsonConfig(config);
converters.add(converter);
}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent 删除jspringboot对象数据 #感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-23 12:35:40
上一篇:【Spring Boot】026
下一篇:【Spring Boot】024
推荐阅读
留言与评论(共有 16 条评论) |
本站网友 脂肪的食物来源 | 9分钟前 发表 |
因此 Gson 的以来添加之后 | |
本站网友 wxwidgets | 21分钟前 发表 |
SerializerFeature.WriteullListAsEmpty | |
本站网友 联想7通道 | 29分钟前 发表 |
如有侵权请联系 cloudcommunity@tencent 删除前往查看jspringboot对象数据 | |
本站网友 缘因我 | 29分钟前 发表 |
因此 Gson 的以来添加之后 | |
本站网友 刘本良 | 2分钟前 发表 |
返回的对象会默认被转换成 JSO 格式;另外 | |
本站网友 陈琨博客 | 22分钟前 发表 |
使用步骤第一步:在 pom.xml 中移除默认的 jackson-databind | |
本站网友 欧莱雅洗发 | 1分钟前 发表 |
@RestController 注解是 @Controller 注解 和 @RespeBody 注解的组合 | |
本站网友 启动项设置 | 5分钟前 发表 |
简介fastjson 是阿里巴巴的一个开源的 JSO 解析框架 | |
本站网友 graves病 | 8分钟前 发表 |
第一种:默认的 jackson-databind1 | |
本站网友 管理软件论坛 | 26分钟前 发表 |
说明默认情况下 | |
本站网友 如何健康减肥 | 11分钟前 发表 |
SerializerFeature.PrettyFormat | |
本站网友 北京二手房网站 | 10分钟前 发表 |
类上使用 @Controller 注解 | |
本站网友 陈明英 | 1分钟前 发表 |
SerializerFeature.WriteullListAsEmpty | |
本站网友 北大国际医院 | 27分钟前 发表 |
类上使用 @Controller 注解 | |
本站网友 上海美食网 | 19分钟前 发表 |
该框架也可以集成到 Spring Boot 中 |