I am receiving json data from a rest call. The keys are all in camel case
.
I am able to obtain this data fine from the rest call. But I wish to convert all these keys to snake case
cos that's
the version I am sending back to the client that needs my response.
In my configuration, I have the following to map snake case.
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES));
return converter;
}
This works if I do not explicitly use @JsonProperty
and also stick with getters
and setters
instead of a builder
.
Example, this would work and give me snake case if my beans are declared in following format.
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Layout {
private final String myBanner;
}
It will not work (will not capture data from rest call) if I use a builder but not use @JsonProperty
as follows.
@Getter
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonDeserialize(builder = Layout.LayoutBuilder.class)
public class Layout {
private final String myBanner;
}
This is what I have now which works but is in camel case. I want snake case.
@Getter
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonDeserialize(builder = Layout.LayoutBuilder.class)
public class Layout {
@JsonProperty("myBanner")
private final String myBanner;
}
I want to stick to using a builder
. Thus question is, is there a way around this to use a builder and still get the values in snake case
for my response.
Or alternative, a way to recursively loop over all fields in an object including nested objects and switch them all out to be snake cased?
Json data from rest call
{
"mainData": {
"groupData": "",
"benefits": {
"summary": {
"title": "",
"shortCopy": ""
}
},
"simpleLayout": {
"myBanner": "summary",
"titles": [
[
"",
""
]
]
},
"maxLayout": {
"myBanner": "summary",
"titles": [
[
""
]
]
}
}
}