当我使用 @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 的情况下全局设置此格式规则。