How to do custom JSON serialization in SpringBoot(RESTful) apps
1. Introduction
This post demonstrates how to implement custom JSON serialization in SpringBoot applications (Spring MVC or RESTful services). By default, SpringBoot uses Jackson to serialize objects. For example:
If we have this object:
And the above object is returned by a SpringBoot Restful service like this:
We run the SpringBoot app and visit http://localhost:8080/myBean
, then we would get this result:
The bornDate field is a Long literal. What if we want it to be in the format MM-dd-yyyy HH:mm:ss, like this:
How can we achieve this?
2. Environments
- SpringBoot 1.x and 2.x
3. Ways to resolve this issue
3.1 Use @JsonFormat
You can use the @JsonFormat
annotation to specify the pattern of the field for custom serialization, as shown below:
3.2 Use Custom JsonSerializer
The @JsonFormat
annotation only changes the field it annotates. If you want to permanently change the serialization of a specific type, you can define a custom JsonSerializer
.
First, define a custom JsonSerializer
by extending the JsonSerializer
class:
Then, add this custom serializer to the ObjectMapper
:
4. The @JsonSerialize(using = CustomDateSerializer.class) Problem
If we use the @JsonSerialize(using = CustomDateSerializer.class)
annotation, we would get this error:
It seems that this annotation is no longer supported by SpringBoot, so be cautious when using it.
5. Summary
This post demonstrated two approaches to implement custom JSON serialization in SpringBoot applications: using @JsonFormat
for field-level customization and creating a custom JsonSerializer
for type-level changes. The example source code is available on GitHub.
Custom JSON serialization is essential for tailoring API responses to specific requirements. By following the methods outlined above, you can ensure your SpringBoot applications serialize data exactly as needed.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 springboot and mvc
- 👨💻 Jackson tutorials
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!