I am using annotated Spring 3.1 MVC code (spring-mvc) and when i send date object through the @RequestBody the date is showing up as numeric. This is my controller
@Controller @RequestMapping("/test") public class MyController { @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true)); } @RequestMapping(value = "/getdate", method = RequestMethod.GET) public @ResponseBody Date getDate(@RequestParam("dt") Date dt, Model model) { // dt is properly constructed here.. return new Date(); } }
When i pass in date, i am able to receive the date in the format. But my browser displays date as numeric
1327682374011
How do i make it display date in the format I have registered for the webbinder? I saw in some forum that I should use jackson mapper, but cant i alter the existing mapper?
There is no date format in JSON, there's only strings a de-/serializer decides to map to date values.
@JsonFormat is a Jackson annotation that we use to specify how to format fields and/or properties for JSON output. Specifically, this annotation allows us to specify how to format Date and Calendar values according to a SimpleDateFormat format.
In order to override the default date formatting strategy of Jakson following are the step to follow:
JsonSerializer
to create a new class for handling date formattingserialize(Date date, JsonGenerator gen, SerializerProvider provider)
function to format date in your desired format and write it back to generator instance (gen)@JsonSerialize(using = CustomDateSerializer.class)
Code:
//CustomDateSerializer class public class CustomDateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = formatter.format(value); gen.writeString(formattedDate); } } //date getter method @JsonSerialize(using = CustomDateSerializer.class) public Date getDate() { return date; }
Source: http://blog.seyfi.net/2010/03/how-to-control-date-formatting-when.html
Alternatively if you are using jackson and want an ISO-8601 date on all dates (not just those you annotate), you can disable the default of writing dates as timestamps.
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/> <bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig" factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" /> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="jacksonSerializationConfig" /> <property name="targetMethod" value="disable" /> <property name="arguments"> <list> <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_DATES_AS_TIMESTAMPS</value> </list> </property> </bean>
Then if you want to convert your dates into some other format than the default, you can do this:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="jacksonSerializationConfig" /> <property name="targetMethod" value="setDateFormat" /> <property name="arguments"> <list> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd'T'HH:mm:ssZ"/> </bean> </list> </property> </bean>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With