我有一个带有 RESTful 端点的 Spring Boot 应用程序,我想为 joda-time 添加自定义序列化程序。但我无法让应用程序默认 Jackson serailzier 识别我的自定义序列。
我使用@RepositoryRestResource 创建了 RESTFul 端点
@RepositoryRestResource(collectionResourceRel = "x", path = "x")
public interface XRepository extends PagingAndSortingRepository<X, Long>
{
}
然后我有一个 GET 调用来返回所有对象 X:
这是我的序列化器:
@Component
public class JsonDateSerializer 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));
}
}
然后我将它添加到属性 Getter 中,如下所示:
@JsonSerialize(using=JsonDateSerializer.class)
public DateTime getDateCreated()
{
return dateCreated;
}
此序列化程序在普通应用程序中完美运行,但当我尝试在我的 Spring Boot 应用程序中使用它时,这些序列化程序将被忽略。
如何让 Spring Boot 识别这些序列化程序?
最佳答案
好吧,经过一番折腾,我找到了答案。我在 joda-datetime 的序列化和反序列化中使用了错误的库。
我在用
org.codehaus.jackson
我应该在什么时候使用
com.fasterxml.jackson
我想这是一个容易犯的错误,因为这两个库具有几乎相同的属性和方法,因为 com.fasterxml.jackson 构建在 org.codehaus.jackson 之上。
现在回头看是个愚蠢的错误,但学到了宝贵的教训,始终检查您是否使用了正确的库!!!!
关于java - Spring Boot 忽略@JsonDeserialize 和@JsonSerialize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30376714/