11

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.

I created RESTFul endpoints using @RepositoryRestResource

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

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

http://localhost:8181/x

This is my serializer:

@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));
}
}

And I add it to the property Getter as follows:

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

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

How can I get Spring Boot to recognize these serializers?

Schokea
  • 708
  • 1
  • 9
  • 28
  • Show us where you try to serialize it. – Sotirios Delimanolis May 21 '15 at 14:32
  • What Jackson version are you trying to use in you `@JsonDeserializer` annotation? – jbarrueta May 21 '15 at 14:38
  • @SotiriosDelimanolis I'm making a POST call using the following JSON {"invoiceId" : "1", "feeds": [{ "dateCreated": "1/1/1"}]} then Spring Boot automatically tries to serialize it. – Schokea May 21 '15 at 14:41
  • @jbarrueta the Jackson version is 2.5.3 – Schokea May 21 '15 at 14:41
  • Are you trying to **de** serialize? – Sotirios Delimanolis May 21 '15 at 14:42
  • @SotiriosDelimanolis I'm trying both to be honest but I just posted 1 problem because if I solve one I'll solve the other it's the same problem as Spring not recoginzing serializers. In this case I was wrong I should have said I'm doing a GET Request. – Schokea May 21 '15 at 14:43
  • So please edit your question to clarify everything. You have a rest endpoint (show it) that receives JSON which you want to deserialize in a custom way with a JSON **deserializer** (not serializer). You currently have shown us a `JsonSerializer`. That's no good if you're trying to deserialize something. – Sotirios Delimanolis May 21 '15 at 14:45
  • @SotiriosDelimanolis sorry my mistake I've edited the question. – Schokea May 21 '15 at 14:57
  • @Schokea Not your question but, you're aware of the joda time extension for jackson, "jackson-datatype-joda"? – ci_ May 21 '15 at 19:27
  • @ci_ yes I'm aware of that extension but that's not the problem. I can persist to the database in other projects but Spring Boot won't recognize my custom serializers. – Schokea May 22 '15 at 06:04

2 Answers2

9

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.

I was using

org.codehaus.jackson

when I should have been using

com.fasterxml.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!!!!

Schokea
  • 708
  • 1
  • 9
  • 28
  • 2
    I did the same thing! Can't believe how easy you can end up using the wrong classes/framework. – Stefan Jan 14 '19 at 15:48
3

With Spring MVC 4.2.1.RELEASE, you need to use the new Jackson2 dependencies as below for the Deserializer to work.

Dont use this

<dependency>  
            <groupId>org.codehaus.jackson</groupId>  
            <artifactId>jackson-mapper-asl</artifactId>  
            <version>1.9.12</version>  
        </dependency>  

Use this instead.

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.2.2</version>
        </dependency>  

Also use com.fasterxml.jackson.databind.JsonDeserializer and com.fasterxml.jackson.databind.annotation.JsonDeserialize for the deserialization and not the classes from org.codehaus.jackson

VimalKumar
  • 1,611
  • 1
  • 14
  • 11