Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用post方法时,使用requestBody接收数据时,如何将接收的实体中字段值为空字符串的字段转为null #2060

Open
OnceCrazyer opened this issue Nov 28, 2023 · 7 comments
Labels
question Further information is requested

Comments

@OnceCrazyer
Copy link

OnceCrazyer commented Nov 28, 2023

请描述您的问题

询问有关本项目的使用和其他方面的相关问题。
使用post方法时且使用requestBody接收数据时,如何将接收的实体中字段值为空字符串的字段值转为null

@OnceCrazyer OnceCrazyer added the question Further information is requested label Nov 28, 2023
@oooopl
Copy link

oooopl commented Nov 29, 2023

自己注册一个String的Reader,在这个 reader里处理

@OnceCrazyer
Copy link
Author

自己注册一个String的Reader,在这个 reader里处理
测试未生效:
ValueFilter valueFilter = (object, name, value) -> {
log.error(name);
if (ObjectUtil.isEmpty(value)) {
value = null;
}
return value;
};
// 自定义格式化输入
fastJsonConfig.setReaderFeatures(JSONReader.Feature.DuplicateKeyValueAsArray, JSONReader.Feature.TrimString);
fastJsonConfig.setReaderFilters(valueFilter);

@oooopl
Copy link

oooopl commented Nov 29, 2023

你这个是 filter不是ObjectReader,读不支持 ValueFilter,你可以看下源码 读仅支持 AutotypeFilter 和 ExtraProcessor

@oooopl
Copy link

oooopl commented Dec 14, 2023

我试了 没生效

@oooopl
Copy link

oooopl commented Dec 15, 2023

目前有个没有办法的解决办法
重写com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter#read方法,手动使用正则表达式去除空串然后 再交由 fastjson2 处理

@oooopl
Copy link

oooopl commented Dec 15, 2023

可以参考正则,但是我觉得很不严谨,也没研究出其它办法
String EMPTY_STR_PATTERN = ",?\"\\w+\":\"\",?"
image

不知道ObjectReaderModule能否实现

@oooopl
Copy link

oooopl commented Dec 19, 2023

舍弃了上面的实现方式。非常不严谨,可以让前端处理

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants