在Spring MVC中使用Jackson JSON时,如何默认启用Pascal大小写? [英] How can I enable Pascal casing by default when using Jackson JSON in Spring MVC?
问题描述
我有一个使用Spring MVC创建和处理多个REST端点的项目。我目前正在使用Jackson自动处理JSON的seralization /反序列化使用 @RequestBody
和 @ResponseBody
注释。
我让杰克逊工作,所以我有一个起点。我的问题是我们的旧序列化手动完成并使用Pascal套管而不是Camel套管(MyVariable而不是myVariable),杰克逊默认使用Camel套管。
我知道我可以使用 @JsonProperty
手动更改变量的名称。话虽这么说,我不认为将 @JsonProperty
添加到我的所有变量中是一个可行的长期解决方案。
除了使用 @JsonProperty
注释之外,有没有办法让Jackson在序列化和反序列化时使用Pascal大小写?
编辑:看起来外部没有干净的方法。有几个人建议重写不同的课程,以此来实现我的目标。我愿意接受有关我可以改写的改变套管的建议。目前我已经制作了一个自定义的ObjectMapper,它设置了我想要的一些属性(即
Inclusion.NON_NULL
)。我还没有找到任何可以让我改变套管行为的地方。有什么想法?我最终通过覆盖(de)序列化器解决了这个问题。对于那些感兴趣的人,您可以自己动手:
步骤1.扩展 BeanSerializerFactory
。
覆盖 _constructWriter(SerializationConfig config,TypeBindings typeContext,PropertyBuilder pb,boolean staticTyping,String name,AnnotatedMember propertyMember)
方法。在该方法中,以您认为合适的任何方式修改 name
。为了实现Pascal大小写,我使用了这一行: String formattedName = name.substring(0,1).toUpperCase()+ name.substring(1);
。修改 name
后,调用 super._constructWriter
。
步骤2.扩展 BeanDeserializationFactory
。
覆盖 constructSettableProperty(DeserializationConfig config,BasicBeanDescription beanDesc,字符串名称,AnnotatedMethod setter)
方法。使用您在自定义 BeanSerializerFactory
中执行的 name
参数执行相同的操作。
步骤3.创建 ObjectMapper
。
将序列化工厂设置为您的自定义豆系列化工厂。设置反序列化器提供程序(我使用了这一行: objectMapper.setDeserializerProvider(new StdDeserializerProvider(new CustomJacksonBeanDeserializerFactory()))
)。
就是这样。您创建的 ObjectMapper
将在序列化或反序列化JSON时使用新的命名方案。
I have a project that uses Spring MVC to create and handle multiple REST endpoints. I'm currently working on using Jackson to automatically handle the seralization/deserialization of JSON using the @RequestBody
and @ResponseBody
annotations.
I have gotten Jackson working, so I've got a starting point. My problem is that our old serialization was done manually and used Pascal casing instead of Camel casing ("MyVariable" instead of "myVariable"), and Jackson does Camel casing by default.
I know that I can manually change the name for a variable using @JsonProperty
. That being said, I do not consider adding @JsonProperty
to all of my variables to be a viable long-term solution.
Is there a way to make Jackson use Pascal casing when serializing and deserializing other than using the @JsonProperty
annotation?
EDIT: It looks like there's not a clean way to do this externally. A couple people have suggested overriding different classes as a way to accomplish my goal. I'm open to suggestions on what I can override that will change the casing. At the moment I have made a custom ObjectMapper that sets some properties I want (namely Inclusion.NON_NULL
). I haven't found any place yet that would let me change the casing behavior. Any thoughts?
I ended up solving the issue by overriding the (de)serializers. For those interested, here's how you can do it yourself:
Step 1. Extend BeanSerializerFactory
.
Override the _constructWriter(SerializationConfig config, TypeBindings typeContext, PropertyBuilder pb, boolean staticTyping, String name, AnnotatedMember propertyMember)
method. Inside that method, modify name
in any way you see fit. To implement Pascal casing, I used this line: String formattedName = name.substring(0, 1).toUpperCase() + name.substring(1);
. After modifying name
, call super._constructWriter
.
Step 2. Extend BeanDeserializationFactory
.
Override the constructSettableProperty(DeserializationConfig config, BasicBeanDescription beanDesc, String name, AnnotatedMethod setter)
method. Do the same thing with the name
parameter that you did inside your custom BeanSerializerFactory
.
Step 3. Create an ObjectMapper
.
Set the serializer factory to be your custom bean serializer factory. Set the deserializer provider (I used this line: objectMapper.setDeserializerProvider(new StdDeserializerProvider(new CustomJacksonBeanDeserializerFactory()))
).
That's it. The ObjectMapper
you created will use your new naming scheme when serializing or deserializing JSON.
这篇关于在Spring MVC中使用Jackson JSON时,如何默认启用Pascal大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!