@ResponseDataを使用すると、カスタムシリアライザーが配置されていても、JodaTimeは完全なオブジェクト状態に変換されます。
構成:
春3.1.2ジャクソン1.9.11
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
カスタムシリアライザー:
public class JodaDateTimeJsonSerializer extends JsonSerializer<DateTime> {
//TODO Bad (hard) code. This should be part of a global system setting through ConfigurationService
private static final String dateFormat = ("dd/MM/yyyy");
private static Logger logger = LoggerFactory.getLogger(JodaDateTimeJsonSerializer.class);
@Override
public void serialize(DateTime date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = DateTimeFormat.forPattern(dateFormat).print(date);
logger.debug("Converted date string: {}", formattedDate);
gen.writeString(formattedDate);
}
}
発車係:
<mvc:annotation-driven />
使用法:
@JsonSerialize(using=JodaDateTimeJsonSerializer.class)
public DateTime getExpiryDate() {
return expiryDate;
}
私が得ている出力はこれに似ています:
"dateCreated":{"monthOfYear":12,"yearOfEra":2012,"yearOfCentury":12,"centuryOfEra":20,"millisOfSecond":359,"millisOfDay":53080359,"secondOfMinute":40,"secondOfDay":53080,"minuteOfHour":44,"minuteOfDay":884,"hourOfDay":14,"weekyear":2012,"weekOfWeekyear":51,"year":2012,"dayOfMonth":19,"dayOfWeek":3,"era":1,"dayOfYear":354,"chronology":{"zone":{"fixed":false,"cachable":false,"id":"Asia/Riyadh"}},"millis":1355917480359,"zone":{"fixed":false,"cachable":false,"id":"Asia/Riyadh"},"afterNow":false,"beforeNow":true,"equalNow":false},"dateModified":{"monthOfYear":12,"yearOfEra":2012,"yearOfCentury":12,"centuryOfEra":20,"millisOfSecond":359,"millisOfDay":53080359,"secondOfMinute":40,"secondOfDa
単純なdd/mm/yyyyの日付が必要な場合。
ご意見をお聞かせください。
さらに、@ JsonSerializeを常に使用する必要がない場合に、この書式設定ルールをグローバルに設定するにはどうすればよいですか。