我有一个带有 RESTful 端点的 Spring Boot 应用程序,我想向其中添加自定义序列化器乔达时间 http://www.joda.org/joda-time/但我无法让应用程序默认 Jackson Serailzier 识别我的自定义 Serailzier。
我使用 @RepositoryRestResource 创建了 RESTFul 端点
@RepositoryRestResource(collectionResourceRel = "x", path = "x")
public interface XRepository extends PagingAndSortingRepository<X, Long>
{
}
然后我有一个 GET 调用来返回所有对象 X:
http://本地主机:8181/x http://localhost:8181/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 识别这些序列化器?