2012-01-27 71 views
29

我使用帶註釋的Spring 3.1 MVC代碼(spring-mvc),當我通過@RequestBody發送日期對象時,日期顯示爲數字。 這是我的控制器Spring 3.1 JSON日期格式

@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(); 
    } 
} 

當我通過日期,我能夠接受的格式的日期。 但我的瀏覽器中顯示日期爲數字

1327682374011 

我如何讓它顯示我已經註冊了webbinder格式的日期? 我在一些論壇看到我應該使用傑克遜映射器,但不能改變現有的映射器?

回答

45

爲了覆蓋Jakson的默認日期格式策略下面是步驟如下:

  1. 擴展JsonSerializer創建一個新的類來處理日期格式
  2. 覆蓋serialize(Date date, JsonGenerator gen, SerializerProvider provider)功能,在日期格式的並將其寫回到生成器實例(gen)
  3. 註釋您的日期getter對象以使用您的擴展json序列化器使用@JsonSerialize(using = CustomDateSerializer.class)

代碼:

//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; 
} 

來源:http://blog.seyfi.net/2010/03/how-to-control-date-formatting-when.html

+0

謝謝Waqas。我想知道默認的JSON序列化器是如何工作的,因爲我沒有在類路徑中使用jackson jar。可能它不使用傑克遜。 – moh 2012-01-31 16:34:33

+3

@moh爲什麼沒有被接受的答案? – 2013-05-16 12:41:30

+0

@ M.AtifRiaz用戶自12月22日18:27以來沒有活動。 :) – Yubaraj 2015-04-21 11:20:07

16

或者,如果您使用的是傑克遜,並希望在所有日期的ISO-8601日期(不只是那些你註釋),您可以禁用寫作的默認日期作爲時間戳。

<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> 

然後,如果你想你的日期轉換成比默認的一些其他的格式,你可以這樣做:

<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> 
5

這裏是配置這更標準的方式,採用ISO8601日期,這是我會爲你的API推薦的。

<!-- you can't call it objectMapper for some reason --> 
<bean name="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> 
    <property name="featuresToDisable"> 
     <array> 
      <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/> 
     </array> 
    </property> 
</bean> 

<!-- setup spring MVC --> 
<mvc:annotation-driven> 
    <mvc:message-converters register-defaults="true"> 
     <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
      <property name="objectMapper" ref="jacksonObjectMapper"/> 
     </bean> 
    </mvc:message-converters> 
</mvc:annotation-driven> 

這是額外的文件: