Spring Boot忽略@JsonDeserialize和@JsonSerialize [英] Spring Boot ignores @JsonDeserialize and @JsonSerialize

查看:3567
本文介绍了Spring Boot忽略@JsonDeserialize和@JsonSerialize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有RESTful端点的Spring Boot应用程序,我想为 joda-添加自定义序列化程序时间但我无法让应用程序默认Jackson serailzier识别我的自定义版本。

I've a Spring Boot application with RESTful endpoints that I want to add custom serializers to for joda-time but I can't get the applications default Jackson serailzier to recognize my custom one's.

我使用@RepositoryRestResource创建RESTFul端点

I created RESTFul endpoints using @RepositoryRestResource

@RepositoryRestResource(collectionResourceRel = "x", path = "x") 
public interface XRepository extends PagingAndSortingRepository<X, Long>
{
}

然后我有一个GET调用来返回所有对象X的:

I then have a GET call to return all object X's:


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,如下所示:

And I add it to the property Getter as follows:

@JsonSerialize(using=JsonDateSerializer.class)
public DateTime getDateCreated()
{
    return dateCreated;
}

这个序列化程序在普通应用程序中完美运行但是当我尝试在我的Spring Boot应用程序会忽略这些序列化程序。

This serializer works perfectly in a normal application but when I try to use this in my Spring Boot application these serializers get ignored.

如何让Spring Boot识别这些序列化程序?

How can I get Spring Boot to recognize these serializers?

推荐答案

好的,经过多次折磨,我找到了答案。我使用错误的库来进行joda-datetime的序列化和反序列化。

Ok so after much torment I found out the answer. I was using the wrong library for the serialization and deserialization of the joda-datetime.

我当时正在使用


org.codehaus.jackson

org.codehaus.jackson

当我应该使用


com.fasterxml。 jackson

com.fasterxml.jackson

我想这是一个容易犯的错误,因为两个库的属性和方法几乎相同,因为com.fasterxml.jackson构建在顶层of org.codehaus.jackson。

I guess this is an easy mistake as both libraries have almost identical properties and methods because com.fasterxml.jackson is built on top of org.codehaus.jackson.

愚蠢的错误现在回顾但是学到的宝贵经验总是检查你使用正确的库!!!!

Silly mistake looking back now but a valuable lesson learnt to ALWAYS check your using the correct library!!!!

这篇关于Spring Boot忽略@JsonDeserialize和@JsonSerialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆