2

I am passing a date parameter inside an object array as request param while making a rest call using RestTemplate to my spring container.

The code is as follows:

HttpEntity<SimplePipeServiceRequest> request = new HttpEntity<>(new Object[]{new Date(), 1}, headers);
response = template.postForEntity(uri1, request, String.class);

When the request comes to my server, i am receiving date as long. How can i get the date as date ? Why is it coming as long ?

Follwing is my dispatcher-servlet.xml

<context:component-scan base-package="com.altra.aligne.controller"></context:component-scan>

<mvc:annotation-driven conversion-service="conversionService">
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.altra.middleware.spring.CustomObjectMapper"></bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<!-- RequestMappingHandlerMapping.setUseSuffixPatternMatch(boolean useSuffixPatternMatch) = Whether to use suffix pattern match (".*") 
when matching patterns to requests. -->
<bean class="com.altra.middleware.spring.ControllerBeanPostProcessor" />

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
  <property name="converters">
    <list>
        <bean class="com.altra.middleware.spring.GenericDTOConverter"/>
        <bean class="com.altra.middleware.spring.StringToDateConverter"/>
    </list>
  </property>
</bean>

and following is the dispatcher-servlet.xml from where i am making the rest call

<mvc:annotation-driven>
    <mvc:message-converters>
        <!-- We configure the Jackson mapper to output dates in ISO801 format. This requires adding our
        customized Jackson mapper to the list of Spring MVC message converters. But, if we just add our bean here
        all by itself, it will handle requests it should not handle, e.g. encoding strings.  So we need to add the
        other standard message converters here too, and make sure to put the customized Jackson converter AFTER the
        string converter. -->

        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
        <ref bean="acmJacksonConverter"/>
        <bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"/>
        <!-- atom feed requires com.sun.syndication package ...   -->
        <!--<bean class="org.springframework.http.converter.feed.AtomFeedHttpMessageConverter"/>-->
        <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.Jaxb2CollectionHttpMessageConverter"/>
        <!-- marshalling converter requires spring oxm -->
        <!--<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"/>-->
    </mvc:message-converters>
</mvc:annotation-driven>

Can anyone explain where and how the date is getting converted to long and if i want to avoid it can i achieve it and if i can get the date equivalent again from long ?

I am not familiar with how actually jackson works with spring.

EDIT

HttpEntity<SimplePipeServiceRequest> request = new HttpEntity<>(AbstractGasService.createSimpleServiceRequest(serviceRequestDTO), headers);
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("GasOpsModule");
module.addSerializer(Date.class, new DateSerializer());
module.addSerializer(Object[].class, new ObjectArraySerializer());
module.addSerializer(String.class, new StringSerializer());
mapper.registerModule(module);
MappingJackson2HttpMessageConverter dateConverter = new MappingJackson2HttpMessageConverter();
dateConverter.setObjectMapper(mapper);
if(!template.getMessageConverters().contains(dateConverter)) {
  template.getMessageConverters().add(dateConverter);
}
response = template.postForEntity(uri1, request, String.class);

Still, neither of the serializers are getting called when the post is executed.

Thanks !

Adithya
  • 2,923
  • 5
  • 33
  • 47

1 Answers1

1

You need to customize ObjectMapper:

ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(DateFormat.getDateInstance());
mapper.setTimeZone(TimeZone.getTimeZone("UTC"));

But if you can, it is much safer to use Java 8 Time package.

For more info, take a look on:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Is there a way i can instruct `RestTemplate` to not convert `date` to `long` as it's getting converted to long when it's converting the parameters to an object array. – Adithya Apr 04 '19 at 13:06
  • can you please paste your Bean class(where you have declared your Date variable), Request and Response that you are sending to RestTemplate? – navis1692 Apr 04 '19 at 14:11
  • @Adithya, you can override message converter used by `RestTemplate`. See this question: [Jackson - Deserializing JSON to class](https://stackoverflow.com/questions/55285941/jackson-deserializing-json-to-class). Or define how `RestTemplate` is created in `Spring` context with right message converter. – Michał Ziober Apr 04 '19 at 14:11
  • Can i create a message converter for `RestTemplate` which will only consider `date` lets say ? – Adithya Apr 04 '19 at 15:17
  • @Adithya, I am not sure what do you mean by `date`? `Date` class or `date` part in `date-time` format? – Michał Ziober Apr 04 '19 at 15:52
  • @MichałZiober - yes. In my object array, if there is a date i want to serialize it to string lets say not long. – Adithya Apr 04 '19 at 16:17
  • just to elaborate - i am passing an `Object[]` which contains multiple data type objects i.e. `Date`, `String`, `Long` etc. For `Date` specifically, i want to handle this operation i.e. either pass `Date` as is or if that's not possible, pass `Long` (which is currently happenin by default) and on my server convert it back to `Date` object. Btw, i added code as shown in *EDIT* section. – Adithya Apr 04 '19 at 16:34