2012-01-27 29 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のために登録した書式で日付を表示するのですか? 私はいくつかのフォーラムでジャクソンマッパーを使うべきだと知っていましたが、既存のマッパーを変更できませんでしたか?次Jaksonのデフォルトの日付書式戦略を上書きするために

答えて

45

はステップが従っている:

  1. オーバーライドserialize(Date date, JsonGenerator gen, SerializerProvider provider)機能で書式の日付に日付の書式設定を処理するための新しいクラスを作成するJsonSerializerを拡張し、あなたの希望の形式とインスタンスを発電機にそれを書き戻す(GEN)
  2. @JsonSerialize(using = CustomDateSerializer.class)
を使用して、拡張JSONシリアライザを使用するように日付ゲッターオブジェクトに注釈を付けます

コード:

//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:ここ

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

追加のドキュメントがあります。私はクラスパスにjackson jarsを持っていないので、春のデフォルトのJSONシリアライザがどのように動作するのか不思議に思っていました。おそらくそれはジャックソンを使用しません。 – moh

+3

@mohなぜ受け入れられた答えはありませんか? –

+0

@ M.AtifRiazユーザーは、18:27時にOct 22 '12からアクティブではありません。 :) – Yubaraj

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>