How to set PropertyNamingStrategy for RestTemplate in SpringBoot?
When creating a RestTemplate
you need to set the objectMapper
to yours.
Also, you should declare your custom ObjectMapper as a @Bean
so it is constructed by Spring as a singleton and managed for you. Do the same for the PropertyNamingStrategy
, instead of 'newing' it up and declaring the class as static.
public class RestConfig
{
/**
* Bean to make jackson automatically convert from
* camelCase (java) to under_scores (json) in property names
*
* @return ObjectMapper that maps from Java camelCase to json under_score names
*/
@Bean
public ObjectMapper jacksonObjectMapper()
{
return new ObjectMapper().setPropertyNamingStrategy(propertyNamingStrategy());
}
@Bean
public PropertyNamingStrategy propertyNamingStrategy()
{
return new UpperCaseUnderscoreStrategy();
}
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
jsonMessageConverter.setObjectMapper(jacksonObjectMapper());
messageConverters.add(jsonMessageConverter);
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}
}
And your class is in a separate file? It doesn't need to be static.
/**
* Property naming strategy that converts both ways between camelCase and under_score
* property names.
*/
public static class UpperCaseUnderscoreStrategy extends PropertyNamingStrategy.PropertyNamingStrategyBase
{
/**
* Converts camelCase to under_score and
* visa versa. The idea is that this
* name strategy can be used for both
* marshalling and unmarshaling.
*
* For example, "userName" would be converted to
* "user_name" and conversely "user_name" would
* be converted to "userName".
*
* @param input formatted as camelCase or under_score string
* @return input converted to opposite format
*/
@Override
public String translate(String input)
{
if (input == null || input.length() == 0)
{
return input; // garbage in, garbage out
}
//
// we always take the first character;
// this preserves initial underscore
//
StringBuilder sb = new StringBuilder();
final int length = input.length();
int i = 0;
//
// skip initial underscores
//
while ((i < length) && ('_' == input.charAt(i)))
{
sb.append(input.charAt(i));
i += 1;
}
while (i < length)
{
//
// find underscores, remove and capitalize next letter
//
while ((i < length) && ('_' != input.charAt(i)) && !Character.isUpperCase(input.charAt(i)))
{
sb.append(input.charAt(i));
i += 1;
}
if(i < length)
{
if('_' == input.charAt(i))
{
// underscore to uppercase
//
// skip underscores
//
while ((i < length) && ('_' == input.charAt(i)))
{
// skip underscores
i += 1;
}
//
// capitalize
//
if (i < length)
{
sb.append(Character.toUpperCase(input.charAt(i)));
i += 1;
}
}
else // uppercase to unscore + lowercase
{
sb.append('_');
sb.append(Character.toLowerCase(input.charAt(i)));
i += 1;
}
}
}
return sb.toString();
}
}
Just add this annotation above POJO's which u will be sending or receiving in ur request response.
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
P.S. the strategy can be different depending upon the requirement.