1

I want to define a custom naming strategy for json. I am trying to configure a different naming strategy in my spring-config.xml. I have added the MappingJackson2HttpMessageConverter as a message convertor with a custom object mapper:

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="com.insta.hms.common.ObjectHttpMessageConverter" />
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="jacksonObjectMapper" />
        </bean>   
        </mvc:message-converters>
    </mvc:annotation-driven>

And I am trying to define an object mapper for the JacksonMessageConvertor here:

<bean id="jacksonObjectMapper" class ="com.fasterxml.jackson.databind.ObjectMapper">
    <property name = "PropertyNamingStrategy" value = "PropertyNamingStrategy.LOWER_CASE" />
</bean>

This is not working; Spring says : Cannot convert value of type 'java.lang.String' to required type 'com.fasterxml.jackson.databind.PropertyNamingStrategy' for property 'PropertyNamingStrategy'

So my eventual aim is to replace 'value' of a propertyNamingStrategy with my custom class which implements propertyNamingStrategy.

I need help with configuring this using XML config.

2 Answers2

0

When you assign a property value = "PropertyNamingStrategy.LOWER_CASE" Spring evaluates it as a String. What you need is:

<bean id="jacksonObjectMapper" class ="com.fasterxml.jackson.databind.ObjectMapper">
    <property name = "propertyNamingStrategy" value = "#{T(com.fasterxml.jackson.databind.PropertyNamingStrategy).LOWER_CASE}" />
</bean>

Also, according to the answers for this question, the following is also possible:

<bean id="jacksonObjectMapper" class ="com.fasterxml.jackson.databind.ObjectMapper">
    <property name = "propertyNamingStrategy" value = "LOWER_CASE" />
</bean>
Community
  • 1
  • 1
Iulian Rosca
  • 1,005
  • 4
  • 15
  • 30
  • I get this error: Cannot resolve reference to bean '#{T(com.fasterxml.jackson.databind.PropertyNamingStrategy).LOWER_CASE}' while setting bean property 'PropertyNamingStrategy'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.fasterxml.jackson.databind.PropertyNamingStrategy$LowerCaseStrategy@76e79f1d' available Neither of these two things work for me.. Any other ideas?? – Aditya Bhatia Mar 30 '17 at 10:59
  • Are you sure you did not use ref="..." instead of "value = "#{T(com.fasterxml.jackson.databind.PropertyNamingStrategy).LOWER_CASE}" ? Can you please update the question with the code that you are trying to use now – Iulian Rosca Mar 31 '17 at 10:27
  • Yeah I was using ref = "..".. I'm able to get it working with value = "..". Second approach isn't still working though – Aditya Bhatia Apr 03 '17 at 06:12
0

Looking at the third code block under section C.2.2.1 found here (property staticField). Names need to be fully qualified.

Jamey
  • 813
  • 1
  • 12
  • 27