如何让杰克逊根据一个简单的模式(如“dd-MM-yyyy”)序列化我的 Joda DateTime 对象?
我试过了:
@JsonSerialize(using=DateTimeSerializer.class)
private final DateTime date;
我也试过:
ObjectMapper mapper = new ObjectMapper()
.getSerializationConfig()
.setDateFormat(df);
谢谢!
使用 Jackson 2.0 和 Joda 模块,这变得非常容易。
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
Maven依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.1.1</version>
</dependency>
代码和文档: https ://github.com/FasterXML/jackson-datatype-joda
二进制文件: http ://repo1.maven.org/maven2/com/fasterxml/jackson/datatype/jackson-datatype-joda/
在您正在映射的对象中:
@JsonSerialize(using = CustomDateSerializer.class)
public DateTime getDate() { ... }
在 CustomDateSerializer 中:
public class CustomDateSerializer extends JsonSerializer<DateTime> {
private static DateTimeFormatter formatter =
DateTimeFormat.forPattern("dd-MM-yyyy");
@Override
public void serialize(DateTime value, JsonGenerator gen,
SerializerProvider arg2)
throws IOException, JsonProcessingException {
gen.writeString(formatter.print(value));
}
}
正如@Kimble 所说,使用Jackson 2,使用默认格式非常容易;只需JodaModule在您的ObjectMapper.
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
对于自定义序列化/反序列化DateTime,您需要实现自己的StdScalarSerializer和StdScalarDeserializer;这很令人费解,但无论如何。
例如,这是一个使用UTC 时区的DateTime序列化程序:ISODateFormat
public class DateTimeSerializer extends StdScalarSerializer<DateTime> {
public DateTimeSerializer() {
super(DateTime.class);
}
@Override
public void serialize(DateTime dateTime,
JsonGenerator jsonGenerator,
SerializerProvider provider) throws IOException, JsonGenerationException {
String dateTimeAsString = ISODateTimeFormat.withZoneUTC().print(dateTime);
jsonGenerator.writeString(dateTimeAsString);
}
}
以及相应的反序列化器:
public class DateTimeDesrializer extends StdScalarDeserializer<DateTime> {
public DateTimeDesrializer() {
super(DateTime.class);
}
@Override
public DateTime deserialize(JsonParser jsonParser,
DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
try {
JsonToken currentToken = jsonParser.getCurrentToken();
if (currentToken == JsonToken.VALUE_STRING) {
String dateTimeAsString = jsonParser.getText().trim();
return ISODateTimeFormat.withZoneUTC().parseDateTime(dateTimeAsString);
}
} finally {
throw deserializationContext.mappingException(getValueClass());
}
}
然后将它们与一个模块联系在一起:
public class DateTimeModule extends SimpleModule {
public DateTimeModule() {
super();
addSerializer(DateTime.class, new DateTimeSerializer());
addDeserializer(DateTime.class, new DateTimeDeserializer());
}
}
然后在您的ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new DateTimeModule());
我遇到过类似的问题,我的解决方案比上面的要清楚得多。
我只是在 @JsonFormat 注释中使用了模式
基本上我的班级有一个DateTime字段,所以我在 getter 周围放了一个注释:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public DateTime getDate() {
return date;
}
我用ObjectMapper
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
ObjectWriter ow = mapper.writer();
try {
String logStr = ow.writeValueAsString(log);
outLogger.info(logStr);
} catch (IOException e) {
logger.warn("JSON mapping exception", e);
}
我们使用杰克逊 2.5.4
https://stackoverflow.com/a/10835114/1113510
尽管您可以为每个日期字段添加注释,但最好为您的对象映射器进行全局配置。如果您使用杰克逊,您可以按如下方式配置您的弹簧:
<bean id="jacksonObjectMapper" class="com.company.CustomObjectMapper" />
<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" >
</bean>
对于 CustomObjectMapper:
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
super();
configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
setDateFormat(new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'ZZZ (z)"));
}
}
当然,SimpleDateFormat 可以使用您需要的任何格式。
同时,当 JodaModule 在类路径中时,Jackson 会自动注册 Joda 模块。我刚刚将 jackson-datatype-joda 添加到 Maven 中,它立即生效。
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.8.7</version>
</dependency>
JSON输出:
{"created" : "2017-03-28T05:59:27.258Z"}
对于那些使用 Spring Boot 的人,您必须将模块添加到您的上下文中,它将像这样添加到您的配置中。
@Bean
public Module jodaTimeModule() {
return new JodaModule();
}
如果你想使用新的 java8 时间模块 jsr-310。
@Bean
public Module jodaTimeModule() {
return new JavaTimeModule();
}
似乎对于 Jackson 1.9.12 默认情况下没有这种可能性,因为:
public final static class DateTimeSerializer
extends JodaSerializer<DateTime>
{
public DateTimeSerializer() { super(DateTime.class); }
@Override
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
if (provider.isEnabled(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS)) {
jgen.writeNumber(value.getMillis());
} else {
jgen.writeString(value.toString());
}
}
@Override
public JsonNode getSchema(SerializerProvider provider, java.lang.reflect.Type typeHint)
{
return createSchemaNode(provider.isEnabled(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS)
? "number" : "string", true);
}
}
此类使用 Joda DateTime 的 toString() 方法序列化数据。
Rusty Kuntz 提出的方法非常适合我的情况。
我正在使用 Java 8,这对我有用。
添加依赖pom.xml
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
并将 JodaModule 添加到您的ObjectMapper
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());