1

Can someone help me here please ?

I need to convert Java 8 LocalDate to JSON and vice versa in my REST application to MVC application where I am calling REST API's. SInce default JacksonJAXbJSONProvider does not parse LOcalDate, I wrote custom Mapper as below And dependency is added in pom.xml

public class LocalDateObjectMapperContextResolver implements ContextResolver<ObjectMapper>{
private final ObjectMapper MAPPER;

public LocalDateObjectMapperContextResolver() {
    MAPPER = new ObjectMapper();
    MAPPER.registerModule(new JavaTimeModule());
    //MAPPER.findAndRegisterModules();
    MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}

@Override
public ObjectMapper getContext(Class<?> type) {
    return MAPPER;
}

}

This was suggested in Java 8 LocalDate Jackson format

I need to pass it to message-converters in spring.xml.

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"
              p:objectMapper-ref="objectMapper"/>
    </mvc:message-converters>
</mvc:annotation-driven>

Since one default mapper is already there, I cannot add my mapper to it. So I made below changes, but it still does not work

<mvc:annotation-driven >
    <mvc:message-converters register-defaults="false">
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
             <!--  p:objectMapper-ref="objectMapper"/> -->
             <property name="objectMapper" ref="localDateMapper" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

This is obviously not correct since I am passing resolver where mapper is required. How do I call getContext() method of custom class and set that in Message Converter ? Now it started throwing exception whilel starting application Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0': Cannot create inner bean 'org.springframework.http.converter.json.MappingJacksonHttpMessageConverter#0' of type [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] while setting bean property 'messageConverters' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.http.converter.json.MappingJacksonHttpMessageConverter#0' defined in ServletContext resource [/WEB-INF/spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '****package***.LocalDateObjectMapperContextResolver' to required type 'org.codehaus.jackson.map.ObjectMapper' for property 'objectMapper'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [****package***.LocalDateObjectMapperContextResolver] to required type [org.codehaus.jackson.map.ObjectMapper] for property 'objectMapper': no matching editors or conversion strategy found

Community
  • 1
  • 1
curious_soul
  • 559
  • 1
  • 8
  • 29

1 Answers1

0

You need only a custom module, not a mapper. Then configure spring accordingly:

<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
      p:simpleDateFormat="yyyy-MM-dd"
      p:modulesToInstall="your.module.JavaTimeModule"/>

<bean id="xmlMapper" parent="objectMapper" p:createXmlMapper="true"/>

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="objectMapper"/>
        </bean>
        <bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter">
            <property name="objectMapper" ref="xmlMapper"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
angcap
  • 149
  • 2
  • 9
  • Thanks for rpely, I still did not get 1. p:modulesToInstall="your.module.JavaTimeModule"/>" This is jsr310 module or the custom module 2. What should go in Module I will be writing 3. p:createXmlMapper="true"/>, Response is in JSON, how to create one for JSON These questions might look silly to experienced developers, excuse me for that, I am new to this. – curious_soul Jun 28 '16 at 12:19
  • If you refer to jackson-datatype-jsr310, in latest version of spring you just need to add the dependency in your pom and the module is automatically detected. To manually add the module, use `` (where p is the [p-namespace](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-p-namespace)). Response depends on the value of Accept Header the client send, if the client send Accept: "text/json" then response is in JSON. – angcap Jun 30 '16 at 10:10
  • I tried above, but there is no property called modulesToInstalled in Jackson2ObjectMapperFactoryBean I got below exception org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapper' defined in ServletContext resource........ Invalid property 'modulesToInstall' of bean class [ org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean]: Bean property 'modulesToInstall' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? – curious_soul Jul 03 '16 at 10:35
  • What is the version of spring you are using?. Could you post your pom.xml (if you have any)? – angcap Jul 03 '16 at 21:37
  • I am using spring 3.2.12. Unfortunately, file is too long to post here. But It contains jackson-jaxrs, jackson-xc from codehaus. I have included jackson-datatype-jsr310 from fasterxml. Is this the reason that Jackson-jaxrs is form codehaus so it is not finding jackson-datatype from newer type. ? If yes, any workaround it, because codehaus is used by other modules of project and If I replace them with newer version, will it break anything in other modules ? – curious_soul Jul 04 '16 at 06:16
  • Solved , follow this link for my solution. http://www.coderanch.com/t/667353/tools/Java-LocalDate-Jackson-format#3114957 – curious_soul Jul 04 '16 at 18:45