如何在SpringBoot中为RestTemplate设置PropertyNamingStrategy?

时间:2016-03-18 23:18:19

标签: java json rest spring-boot resttemplate

我编写了一个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();
            }
        }

当我的休息服务将Java pojos转换为json以获得响应时,我可以看到命名策略的translate方法被调用。然而,当我通过RestTemplate消耗休息api时,我不会看到这个被调用,并且我生成的pojos没有从传入的json正确初始化;名称需要翻译的所有属性都为null。这迫使我在大多数属性上使用@JsonProperty。我有很多属性和很多pojos - 这是SpringBoot应该帮助的一种非常不优雅的样板解决方案。有没有办法设置一个PropertyNamingStrategy,RestTemplate将用于将传入的json名称从under_score转换为camelCase?

感谢您的帮助。

4 个答案:

答案 0 :(得分:6)

创建RestTemplate时,您需要将objectMapper设置为您的@Bean。 此外,您应该将自定义ObjectMapper声明为PropertyNamingStrategy,因此它由Spring构建为单例并为您管理。对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; } } 执行相同操作,而不是“新增&#39;它并将该类声明为静态。

    /**
     * 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();
        }
    }

你的班级是在一个单独的档案中吗?它不需要是静态的。

{{1}}

答案 1 :(得分:2)

只需在您将在您的请求响应中发送或接收的POJO上方添加此注释即可。

@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)

P.S。根据需求,策略可能会有所不同。

答案 2 :(得分:1)

一个简短的答案是使用Spring的objectMapper。好处是它在application.properties中共享相同的配置。因此,您可以在其中设置spring.jackson.property-naming-strategy=SNAKE_CASE或其他任何值,并且它在包括RestTemplate在内的整个应用程序中都是一致的。代码如下。

@Configuration
@RequiredArgsConstructor
public class HTTPConfig {
    public final ObjectMapper objectMapper;  // provided by spring

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplateBuilder()
            .messageConverters(new MappingJackson2HttpMessageConverter(objectMapper))
            .build();
    }
}

答案 3 :(得分:0)

我建议你使用这种方法。您也可以将其添加为 spring bean。

private RestTemplate getRestTemplate() {

    final RestTemplate restTemplate = new RestTemplate();
    final List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    final MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonMessageConverter.setObjectMapper(new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE));
    messageConverters.add(jsonMessageConverter);
    restTemplate.setMessageConverters(messageConverters);

    return restTemplate;
}