Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Different Deserialization vs Serlialization Method

My Spring Boot API uses camelCase, but I need to proxy some requests through my API to a 3rd party API that uses snake_case. Is it possible to configure Jackson to deserialize the 3rd party response from snake_case, then serialize it back to camelCase to my frontend?

Step by step example of desired functionality:

Example Object:

MyObject {
  String myProperty;
}
  • I call my API
  • API calls 3rd Party
  • 3rd Party returns
{
  "my_property": "my value"
}
  • My API deserializes it into MyObject
  • My API serializes the object and returns
{
  "myProperty": "my value"
}

Right now I am using @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) but of course that will serialize into snake_case as well. Note - even though my api uses camelCase, it would be acceptable to always deserialize from snake_case as this will be a readonly enpoint.

like image 842
grizzasd Avatar asked Sep 17 '25 11:09

grizzasd


1 Answers

You could add the @JsonAlias annotation on the individual properties to add alternative names for deserialization. Or you could configure multiple object mappers with explicit naming strategies, one for deserialization of this third-party API, and one for your normal serialization/deserialization.

like image 96
Mark Rotteveel Avatar answered Sep 19 '25 01:09

Mark Rotteveel