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 !