Spring Rest Controller handling java.util.Optional using Jackson -
i have got spring restcontroller classes handle rest services using json. json using jackson. there fields of java.util.optional type
private optional<long> start = optional.empty();
to enable handling of optional type, configuring spring follows
<bean id="objectmapper" class="org.springframework.http.converter.json.jackson2objectmapperfactorybean"> <property name="modulestoinstall" value="com.fasterxml.jackson.datatype.jdk8.jdk8module" /> </bean>
however when call webservice, fails in deserializing optional types following message
org.springframework.http.converter.httpmessagenotreadableexception: not read json: can not instantiate value of type [simple type, class java.util.optional<java.lang.long>] long integral number (3424323423432); no single-long-arg constructor/factory method
doing serialization/deserialization stand alone code works fine. there register module directly using following code
objectmapper m = new objectmapper(); m.registermodule(new jdk8module());
versions using:
spring : 4.1.5.release
jackson: 2.5.1
thanks in advance
i figure out this. solution register object mapper message converter follows
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.mappingjackson2httpmessageconverter"> <property name="objectmapper" ref="objectmapper" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="objectmapper" class="org.springframework.http.converter.json.jackson2objectmapperfactorybean"> <property name="modulestoinstall" value="com.fasterxml.jackson.datatype.jdk8.jdk8module" /> </bean>
Comments
Post a Comment