Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. @Controller
  2. @RequestMapping("/test")
  3. public class MyController {
  4.  
  5. @InitBinder
  6. public void initBinder(WebDataBinder binder) {
  7. binder.registerCustomEditor(Date.class,
  8. new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
  9. }
  10.  
  11.  
  12. @RequestMapping(value = "/getdate", method = RequestMethod.GET)
  13. public @ResponseBody Date getDate(@RequestParam("dt") Date dt, Model model) {
  14. // dt is properly constructed here..
  15. return new Date();
  16. }
  17. }
  18.  
  19. 1327682374011
  20.  
  21. //CustomDateSerializer class
  22. public class CustomDateSerializer extends JsonSerializer {
  23. @Override
  24. public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) throws
  25. IOException, JsonProcessingException {
  26.  
  27. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  28. String formattedDate = formatter.format(value);
  29.  
  30. gen.writeString(formattedDate);
  31.  
  32. }
  33. }
  34.  
  35.  
  36. //date getter method
  37. @JsonSerialize(using = CustomDateSerializer.class)
  38. public Date getDate() {
  39. return date;
  40. }
  41.  
  42. <bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
  43. <bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig" factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
  44. <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  45. <property name="targetObject" ref="jacksonSerializationConfig" />
  46. <property name="targetMethod" value="disable" />
  47. <property name="arguments">
  48. <list>
  49. <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_DATES_AS_TIMESTAMPS</value>
  50. </list>
  51. </property>
  52. </bean>
  53.  
  54. <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  55. <property name="targetObject" ref="jacksonSerializationConfig" />
  56. <property name="targetMethod" value="setDateFormat" />
  57. <property name="arguments">
  58. <list>
  59. <bean class="java.text.SimpleDateFormat">
  60. <constructor-arg value="yyyy-MM-dd'T'HH:mm:ssZ"/>
  61. </bean>
  62. </list>
  63. </property>
  64. </bean>
  65.  
  66. <!-- you can't call it objectMapper for some reason -->
  67. <bean name="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
  68. <property name="featuresToDisable">
  69. <array>
  70. <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/>
  71. </array>
  72. </property>
  73. </bean>
  74.  
  75. <!-- setup spring MVC -->
  76. <mvc:annotation-driven>
  77. <mvc:message-converters register-defaults="true">
  78. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  79. <property name="objectMapper" ref="jacksonObjectMapper"/>
  80. </bean>
  81. </mvc:message-converters>
  82. </mvc:annotation-driven>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement