SpringBoot序列化、反序列化空字符串为null的三种方式
一、需求:接收前端传入的""空字符串参数,有时候我们需要把它转为null
- SpringBoot项目
- 方式:①Jackson(推荐)、②切面+反射、③注解+切面+反射
- 后两种方式,未做返回值的处理。
二、三种方式
1、Jackson正反序列化(推荐)
- StdConverter 和 JsonSerializer的区别
两种方式都可以实现将空字符串修改为 null 值的逻辑,但它们之间有一些区别:
1. **继承的类不同**:
- 使用 `StdConverter` 方式时,`StringToNullSerializer` 类继承自 `StdConverter<String, String>`,并实现了 `convert` 方法。
- 使用 `JsonSerializer` 方式时,`StringToNullSerializer` 类直接继承自 `JsonSerializer<String>`,并实现了 `serialize` 方法。
2. **接口和方法不同**:
- `StdConverter` 是 Jackson 库中提供的用于定义转换器的类,其中的 `convert` 方法用于将一个类型转换为另一个类型。
- `JsonSerializer` 是 Jackson 库中用于定制序列化逻辑的接口,其中的 `serialize` 方法用于将 Java 对象序列化为 JSON 数据。
3. **对于序列化过程的处理不同**:
- 在 `StdConverter` 方式中,你需要实现 `convert` 方法来定义如何将空字符串转换为 null 值。
- 在 `JsonSerializer` 方式中,你需要实现 `serialize` 方法来定义如何将字段序列化为 JSON 数据,并在其中进行空字符串转换为 null 值的处理。
综上所述,两种方式都可以实现相同的功能,选择哪一种方式取决于个人偏好以及代码的整体结构和风格。通常来说,如果只需要定制序列化逻辑而不需要转换其他类型,直接实现 `JsonSerializer` 接口可能会更清晰和简洁。
- ENTITY
package com.cc.jxtd.entity;
import com.cc.jxtd.serializer.ConverterEmptyStringToNull;
import com.cc.jxtd.serializer.EmptyStringToNullDeserializer;
import com.cc.jxtd.serializer.ConverterEmptyStringToInteger0;
import com.cc.jxtd.serializer.EmptyStringToNullSerializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import javax.naming.Name;
/**
* <p>请求,返回都用这个</p>
*
* @author --
* @since 2024/4/19
*/
@Data
public class UserCs {
private Long id;
//反序列化(前端请求):空字符串为null
@JsonDeserialize(using = EmptyStringToNullDeserializer.class)
private String name;
//反序列化(前端请求):转换:为其他类型的值(转换为int的0)
@JsonDeserialize(converter = ConverterEmptyStringToInteger0.class)
private Integer descConverter0;
//序列化(后端返回):空字符串为null
@JsonSerialize(using = EmptyStringToNullSerializer.class)
private String descSerialize;
//序列化(后端返回):转换:空字符串转为null
@JsonSerialize(converter = ConverterEmptyStringToNull.class)
private String descConverterNull;
}
- 序列化处理类
package com.cc.jxtd.serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/** 序列化:String的空转为null
* @author --
* @since 2024/4/18
**/
public class EmptyStringToNullSerializer extends JsonSerializer<String> {
/**
* 序列化为null
*/
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
if (value == null || value.trim().isEmpty()) {
gen.writeNull();
}else {
gen.writeString(value);
}
}
}
- 反序列化处理类
package com.cc.jxtd.serializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
/** 反序列化:空字符串转换为null
* @author --
* @since 2024/4/18
**/
public class EmptyStringToNullDeserializer extends JsonDeserializer<String> {
/**
* 反序列化为null
*/
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getValueAsString();
if (value == null || value.trim().isEmpty()) {
return null;
}
return value;
}
}
- 序列化-转换1
package com.cc.jxtd.serializer;
import com.fasterxml.jackson.databind.util.StdConverter;
/** 序列化-转换:将string的空转为null
* @author --
* @since 2024/4/18
**/
public class ConverterEmptyStringToNull extends StdConverter<String, String> {
@Override
public String convert(String value) {
//把空的string转为int的0
if (value == null || value.trim().isEmpty()) {
return null;
}
return value;
}
}
- 序列化-转换2
package com.cc.jxtd.serializer;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.util.StdConverter;
/** 序列化:1将string转为int。
* 2转换String的空或null -》 转为Integer的0
* @author --
* @since 2024/4/18
**/
public class ConverterEmptyStringToInteger0 extends StdConverter<String, Integer> {
@Override
public Integer convert(String value) {
//把空的string转为int的0
if (value == null || value.trim().isEmpty()) {
return 0;
}
return Integer.valueOf(value);
}
}
- Controller
package com.cc.jxtd.web.controller;
import com.cc.jxtd.entity.UserCs;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p></p>
*
* @author --
* @since 2024/4/19
*/
@RestController
@RequestMapping("/userCs")
public class UserController {
@PostMapping
public UserCs get(@RequestBody UserCs req){
System.out.println("请求参数-id:" + req.getId());
System.out.println("请求参数-name:" + req.getName());
System.out.println("请求参数-desc1:" + req.getDescSerialize());
System.out.println("请求参数-desc2:" + req.getDescConverterNull());
System.out.println("请求参数-desc3:" + req.getDescConverter0());
//返回:序列化
return req;
}
}
- 测试
2、切面+反射/3、注解+切面+反射
- 区别
2、切面+反射:所有空字符串的字段都转为null
3、注解+切面+反射:只有打了@EmptyToNull的字段才会转换
- 导入包
<!--spring-boot-starter-aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
- 切面
package com.cc.jxtd.aspect;
import com.cc.jxtd.annotation.EmptyToNull;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Objects;
/** 切面
* @author --
*/
@Aspect
@Component
public class UpdateAspect {
private static final Logger logger = LoggerFactory.getLogger(UpdateAspect.class);
//切入点
@Pointcut("@annotation(com.cc.jxtd.annotation.OptConverter)")
public void validPointCut() {
}
/**
* 环绕修改参数
*/
@Around("validPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
Object[] args = point.getArgs();
Object arg = args[0];
//2、切面+反射:全部转换
this.allEmptyToNull(arg);
//3、注解+切面+反射:部分转换
// this.assignEmptyToNull(arg);
return point.proceed();
}
/**
* 设置请求参数中 所有字段的空值(如:String的空字符串)为null
* @param arg arg
*/
public void allEmptyToNull(Object arg) {
if (Objects.isNull(arg)) {
return;
}
Field[] fields = arg.getClass().getDeclaredFields();
for (Field field : fields) {
// 设置字段可访问
field.setAccessible(true);
// 如果字段是 String 类型且值为空字符串,则设置为 null
if (field.getType() == String.class) {
try {
String value = (String) field.get(arg);
if (value != null && value.isEmpty()) {
field.set(arg, null);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
// 可以扩展其他类型的参数……
}
}
/** 指定空转null
* @param arg arg
* @since 2024/4/18
**/
private void assignEmptyToNull(Object arg) {
if (Objects.isNull(arg)) {
return;
}
Field[] fields = arg.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(EmptyToNull.class)) {
// 设置字段可访问
field.setAccessible(true);
// 如果字段是 String 类型且值为空字符串,则设置为 null
if (field.getType() == String.class) {
try {
String value = (String) field.get(arg);
if (value != null && value.isEmpty()) {
field.set(arg, null);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
// 可以扩展其他类型的参数……
}
}
}
}
- 注解
package com.cc.jxtd.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 转换
* @author --
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OptConverter {
}
- 注解2
package com.cc.jxtd.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** 转化空为null
* @author --
* @since 2024/4/18
**/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EmptyToNull {
}
- entity
package com.cc.jxtd.entity;
import com.cc.jxtd.serializer.ConverterEmptyStringToInteger0;
import com.cc.jxtd.serializer.ConverterEmptyStringToNull;
import com.cc.jxtd.serializer.EmptyStringToNullDeserializer;
import com.cc.jxtd.serializer.EmptyStringToNullSerializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
/**
* <p>请求,返回都用这个</p>
*
* @author --
* @since 2024/4/19
*/
@Data
public class UserCs2 {
private Long id;
private String name;
private String desc;
}
- controller
package com.cc.jxtd.web.controller;
import com.cc.jxtd.annotation.OptConverter;
import com.cc.jxtd.entity.UserCs;
import com.cc.jxtd.entity.UserCs2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p></p>
*
* @author --
* @since 2024/4/19
*/
@RestController
@RequestMapping("/userCs")
public class UserController {
// @PostMapping
// public UserCs get(@RequestBody UserCs req){
// System.out.println("请求参数-id:" + req.getId());
// System.out.println("请求参数-name:" + req.getName());
// System.out.println("请求参数-DescSerialize:" + req.getDescSerialize());
// System.out.println("请求参数-DescConverterNull:" + req.getDescConverterNull());
// System.out.println("请求参数-DescConverter0:" + req.getDescConverter0());
//
// //返回:序列化
// return req;
// }
@OptConverter
@PostMapping
public UserCs2 get(@RequestBody UserCs2 req){
System.out.println("请求参数-id:" + req.getId());
System.out.println("请求参数-name:" + req.getName());
System.out.println("请求参数-desc:" + req.getDesc());
//返回:序列化
return req;
}
}
测试2
测试3
SpringBoot序列化、反序列化空字符串为null的三种方式的更多相关文章
- C# 对象与JSON字符串互相转换的三种方式
C# 对象与JSON字符串互相转换的三种方式 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 关于内存对象和JSON字符串的相互转换, ...
- JSON字符串互相转换的三种方式和性能比较
C# 对象与JSON字符串互相转换的三种方式 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 关于内存对象和JSON字符串的相互转换, ...
- 总结springboot开启mybatis驼峰命名自动映射的三种方式
方式一:通过springboot的配置文件application.yml mybatis: configuration: map-underscore-to-camel-case: true 此方式是 ...
- SpringBoot:CORS处理跨域请求的三种方式
一.跨域背景 1.1 何为跨域? Url的一般格式: 协议 + 域名(子域名 + 主域名) + 端口号 + 资源地址 示例: https://www.dustyblog.cn:8080/say/Hel ...
- js中将字符串转为JSON的三种方式
1.eval方式解析,恐怕这是最早的解析方式了.如下: function strToJson(str){ var json = eval('(' + str + ')'); return json; ...
- Java空字符串与null的区别和判断字符串是否为空的方法
Java空字符串与null的区别: 1.类型null表示的是一个对象的值,而并不是一个字符串.例如声明一个对象的引用,String a = null ;""表示的是一个空字符串,也 ...
- Oracle坑之-空字符串与NULL
空字符串与NULL 首先有如下代码 SELECT * FROM Pdc_DataDomain DD INNER JOIN Pdc_DD_Table DDT ON DD.DataDomainID = D ...
- SpringMVC空字符串转为null
空字符串转为null 现在我遇到这样一个需求,那就是我想要吧前端传过来的值变为空,因为所谓前端的校验,其实都不是校验,如果前端传给后台一个表单,可是表单未填入值,我们后台进行判断的时候 既需要判断nu ...
- Java进阶(二十一)java 空字符串与null区别
java 空字符串与null区别 1.类型 null表示的是一个对象的值,而并不是一个字符串.例如声明一个对象的引用,String a = null ; ""表示的是一个空字符串, ...
- Access空字符串和Null值
什么是空字符串和Null值: Microsoft Access可以区分两种类型的空值.因为在某些情况下,字段为空,可能是因为信息目前无法获得,或者字段不适用于某一特定的记录.例如,表中有一个“电话号码 ...
随机推荐
- Eagle+欧奥PicHome创建私有的pinterest网站
Pinterest和花瓣网, 是设计师寻找灵感的天堂!它能够帮你采集.存储和发现灵感.可以说是设计师必用的网站. Eagle是设计师个人的灵感收集工具,它能够方便的采集素材,并快速为图片标签,分类,评 ...
- FastJson反序列化3-1.2.25绕过
在1.2.25中,主要添加了config.checkAutoType(typeName, null)函数,所以从这里开始查看检查逻辑: 为了方便,先看POC: public void byPass1( ...
- NetAdapt:MobileNetV3用到的自动化网络简化方法 | ECCV 2018
NetAdapt的思想巧妙且有效,将优化目标分为多个小目标,并且将实际指标引入到优化过程中,能够自动化产生一系列平台相关的简化网络,不仅搜索速度快,而且得到简化网络在准确率和时延上都于较好的表现 ...
- python 1992和2006年国家标准学科分类和代码标准化并存入MySQL数据库
数据表 代码 1 import pandas as pd 2 import pymysql 3 4 5 def get_subject_1992(): 6 res={} 7 the_former_co ...
- 从 findbugs-maven-plugin 到 spotbugs-maven-plugin 帮你找到代码中的bug
一.findbugs-maven-plugin 介绍: Status: Since Findbugs is no longer maintained, please use Spotbugs whic ...
- 浅谈ET框架--ECS设计核心(一)
ET框架的ECS设计核心可以总结为一句话,那就是: 继承转组件,多态转分发 OOP设计里的继承更换为组件Component模式,多态转成分发模式. 框架代码里头的案例: 数值组件挂载Entity上. ...
- 看你能解锁哪些新身份?OpenHarmony大使、MVP、金码达人在线申报
- Redis 19 SpringBoot集成
概述 SpringBoot 整合 Redis 是使用 SpringData 实现的. SpringData 是与 SpringBoot 齐名的顶级项目,整合了对常用数据库的模板型操作. 在 Sprin ...
- VS 在 Release 模式下使用断点调试程序
修改方法: 项目属性 --> c/c++ --> 常规 --> 调试信息格式 选择程序数据库 (默认:无) 项目属性 --> c/c++ --> 优化 --> 优化 ...
- 安装HTMLTestRunner库
安装 HTMLTestRunner 库的方法非常简单,直接 pip 就可以了 pip install html-testRunner 在 https://pypi.org/ 中可以直接搜索到,并且官 ...