0

There is no error caused by the configuration below. However it is not taken efect in Json converter. I am using MethodInvokingBean instead of MethodInvokingFactoryBean because I read that the second one give me a new instance and I am interested in changing the current instance used by RestTemplate. Anyway, I tried the factory one and it doesn't take effect as well. Honestly, after some weeks searching for, I am wondering if it is really possible to change the default configuration of MapperObject when it is used by RestTemplate. The default configuration is to ignore the nanoseconds and I am really asking myself how someone else has been using RestTemplate when there is the requirement of nanoseconds. I can fix this by changing from sql.TimeStamp to String but it doesn't seem the best approach. If someone else has faced similiar issue and has been able to use sql.TimeStamp with nanoseconds either by changing Jackson Mapper configuration or other way I will appreciate a lot some tips.

//everything start here

ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
_myCllientOfRestService = context.getBean(MyCllientOfRestService.class, request, response);
_myCllientOfRestService.myMethod();

// myCllientOfRestService

Public void myMethod(){
       //it is not returning the nano seconds and I am sure the nano seconds is available in rest service return side. MyReturnType object has the sql.TimeStamp variable filled in with nanoseconds in the rest service side but it is lost in client side
       _myReturnType = restTemplate.postForObject(urlRestService, myParameters,MyReturnType.class);
}

//applicationContext.xml

<bean id="myCllientOfRestService" class="com.someCompany.mhe.log.handler.MyCllientOfRestService" scope="prototype" lazy-init="true">
 <property name="restTemplate" ref="restTemplate2" />
</bean>

      <bean id="myMIB"
          class="org.springframework.beans.factory.config.MethodInvokingBean">
          <property name="targetObject" ref="jacksonObjectMapper" />
          <property name="targetMethod" value="configure" />
          <property name="arguments">
              <list>
                  <value type="com.fasterxml.jackson.databind.SerializationFeature">WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS</value>
                  <value>true</value>
              </list>

          </property>
      </bean>

      <bean id="myMIB2"
          class="org.springframework.beans.factory.config.MethodInvokingBean">
          <property name="targetObject" ref="jacksonObjectMapper" />
          <property name="targetMethod" value="configure" />
          <property name="arguments">

              <list>
                  <value type="com.fasterxml.jackson.databind.DeserializationFeature">READ_DATE_TIMESTAMPS_AS_NANOSECONDS</value>
                  <value>true</value>
              </list>
          </property>
      </bean>

      <bean id="restTemplate2" class="org.springframework.web.client.RestTemplate" >
          <property name="messageConverters">
              <list>
                  <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                      <property name="objectMapper" ref="jacksonObjectMapper" />
                  </bean>
              </list>
          </property>
      </bean>
erhun
  • 3,549
  • 2
  • 35
  • 44
Jim C
  • 3,957
  • 25
  • 85
  • 162
  • Did you check JAXB, we use it in our project this is not and an answer but can be a tip, please check http://stackoverflow.com/questions/14734741/converting-json-to-xml-and-vice-versa-using-jackson-api-and-jaxb-annotation and ask for further question – erhun Jan 06 '15 at 21:34
  • If I understood correctly you are suggesting me to take Jackson out from my project classpath and add Jaxb. I am not using xml at all. I am only developing Rest Web Service to work with Json. Will that work with Jaxb? Even more important, will RestTemplate work with Jaxb and I will be able to get the nanoseconds from sql.Timestamp variable? I can remove RestTemplate and evoke the rest service from HttpClient or wherever recommended object as long as I can get the nanoseconds. – Jim C Jan 06 '15 at 23:59
  • I don't know is it working with nanoseconds, but JAXB is not only an XML mapper you can use it to map JSON too, because of that i suggest to use it. You can check http://www.mkyong.com/webservices/jax-rs/download-json-from-jax-rs-with-jaxb-resteasy/ – erhun Jan 07 '15 at 00:08

0 Answers0