How can I enable Pascal casing by default when using Jackson JSON in Spring MVC?

安稳与你 提交于 2019-12-22 07:08:25

问题


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?


回答1:


See http://www.cowtowncoder.com/blog/archives/2011/03/entry_448.html If you can wait for 1.8 it will be included there.




回答2:


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.




回答3:


For what it's worth, there is a Jira issue to support pluggable strategies; voting for it might help convince developers to add support. As I mentioned in the comment, it is possible to override internal behavior, but it is not a simple thing to do.



来源:https://stackoverflow.com/questions/4670116/how-can-i-enable-pascal-casing-by-default-when-using-jackson-json-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!