Ezw*_*ard 5 java rest json resttemplate spring-boot
我编写了一个SpringBoot应用程序,它使用了一个rest api并提供了一个rest api.我的模型pojo有camelCase命名属性.应用程序消耗的json具有under_score属性名称.应用程序生成的json具有under_score属性名称.我想使用PropertyNamingStrategy,它将在编组/解组期间自动在Java和json名称之间进行转换.
我有Java配置,试图设置一个可以处理这个的命名策略;
/**
* Configuration for Rest api.
* <p>
* Created by emurphy on 2/25/16.
*/
@Configuration
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(new UpperCaseUnderscoreStrategy());
}
/**
* 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();
}
}
Run Code Online (Sandbox Code Playgroud)
当我的休息服务将Java pojos转换为json以获得响应时,我可以看到命名策略的translate方法被调用.但是,当我通过RestTemplate消耗一个rest api时,我看不到这个被调用,并且我得到的pojos没有从传入的json中正确初始化; 名称需要翻译的所有属性都为null.这迫使我在大多数属性上使用@JsonProperty.我有很多属性和很多pojos - 这是SpringBoot应该帮助的一种非常不优雅的样板解决方案.有没有办法我可以设置一个PropertyNamingStrategy,RestTemplate将用于将传入的json名称从under_score转换为camelCase?
谢谢你的帮助.
在创建时,RestTemplate您需要将其设置objectMapper为您的.此外,您应该声明自定义ObjectMapper,@Bean因此它由Spring构建为单例并为您管理.为此做同样的事情PropertyNamingStrategy,而不是"新近"它并将该类声明为静态.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
你的班级在一个单独的档案中?它不需要是静态的.
/**
* 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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6577 次 |
| 最近记录: |