杰克逊 - 忽略蛇盒钥匙的情况 [英] jackson - ignoring case in snake case keys

查看:122
本文介绍了杰克逊 - 忽略蛇盒钥匙的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求使用包含下划线的键接收JSON,甚至忽略单词中的大小写。对于例如Device_control_API,device_control_API,Device_Control_API,device_control_aPI等都应映射到同一属性。

I have a requirement to receive JSON with keys that contain underscore and even ignore the case in words. For e.g. Device_control_API, device_control_API, Device_Control_API, device_control_aPI etc all should map to same property.

现在我知道我可以使用@JsonSetter创建多个setter方法,并且所有组合都可以,但是我不要以为那会好。

Now I know that I can create multiple setter methods using @JsonSetter with all combinations possible, but I don't think that will be good.

我见过其他问题建议使用 mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,true)
对于ObjectMapper对象忽略大小写,但我不能这样做,因为我使用spring-boot并希望我的REST API以POJO对象的形式获取有效负载。

I have seen other questions which suggest using mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true) for ObjectMapper object to ignore case, but I can't do that because I am using spring-boot and want my REST API to get payload in the form POJO object.

是否有任何注释或某种方式这样做

Is there any annotation or some way to do so

请帮助!!!

推荐答案

我认为你不能使用 MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES 作为注释。从此处获得以下信息

I dont think you can use the MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES as annotation. Got the following information from here


杰克逊开/关功能:MapperFeature

杰克逊定义了一套mapper配置,在使用ObjectMapper之前只能定义
- 这意味着这些设置可以在每个请求的基础上动态更改
。它们配置
基本的POJO内省细节,并且生成的对象
(序列化器,反序列化器,相关)被高度缓存。如果你需要
不同的设置,你必须使用单独的ObjectMapper
实例。

Jackson defines a set of per-mapper configuration, which can ONLY be defined before using ObjectMapper -- meaning that these settings can not be changed on-the-fly, on per-request basis. They configure fundamental POJO introspection details, and resulting built objects (serializers, deserializers, related) are heavily cached. If you need differing settings for these, you have to use separate ObjectMapper instances.

MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES 是其中一个配置。

但也许自定义反序列化类可以帮助你。 Stackoverflow上有很多教程和问题。

But maybe a custom deserialization class could help you. There are many tutorials and questions on Stackoverflow.

我找到了一些:

在Spring中编写JSON反序列化程序或扩展的正确方法它

这篇关于杰克逊 - 忽略蛇盒钥匙的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆