Skip to content
Advertisement

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.

Advertisement

Answer

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.

5 People found this is helpful
Advertisement