@JsonSerialize (2.8.8.1) with custom JsonSerializer implementation does not work in Apache TomEE

16 views
Skip to first unread message

Serban Alexe

unread,
May 31, 2019, 6:29:13 PM5/31/19
to jackson-user
I have an app deployed inside an Apache TomEE server. At some point, I use a class with a custom JsonSerializer attached to a Date field, so that I can control the format in which the Date is serialized.

If I invoke ObjectMapper().writeValueAsString in a sample main method, the JsonSerializer works fine.

HOWEVER, it has no effect in the app when I deploy it in the TomEE server.
There are no errors at the server startup.

This is what I have in the pom.xml:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.8.1</version>
    <scope>compile</scope>
</dependency>


And these three files are deployed in the app's lib folder: jackson-annotations-2.8.0.jar, jackson-core-2.8.8.jar, jackson-databind-2.8.8.1.jar.

Here are the relevant code portions:

import com.fasterxml.jackson....
// YES, all JSON-related stuff is from fasterxml

@JsonAutoDetect
public class Item {
    private Date lastModified;

    @JsonSerialize(using = CSer.class)
    public Date getLastModified() {
        return lastModified;
    }
}

public class CSer extends JsonSerializer<Date> {
    public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    @Override
    public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
            throws IOException, JsonProcessingException {
        gen.writeString(dateFormat.format(value));
    }
}

// some place else, in a REST service class
...
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getItems(... {
    ...
    return Response.ok(result.getData()).build();
    // result.getData() is an ArrayList of "Item" objects.
}

And here is the main method where the JsonSerializer works fine:

public static void main(String[] args) throws JsonProcessingException {
    Item it = new Item();
    it.setLastModified(new Date());
    String s = new ObjectMapper().writeValueAsString(it);
    System.out.println(s);
}

Help is much appreciated, I'm stuck with this issue...

Thank you.

Raj Kundalia

unread,
Jul 2, 2019, 11:07:27 AM7/2/19
to jackson-user
Hi,

I believe that this has nothing to do with Tomcat.

I am using:

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.9.8</version>

</dependency>


I am using a class with a custom JsonSerializer attached to a Date field and it is working as expected.

JsonDateSerializer.java

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;


import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.SerializerProvider;


public class JsonDateSerializer extends JsonSerializer<Date> {

@Override

public void serialize(Date arg0, JsonGenerator gen, SerializerProvider provider) throws IOException {

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");

String formattedDate = sdf.format(arg0);

gen.writeString(formattedDate);

}

}

On top of the field that I want to serialize, I am using @JsonSerialize(using = JsonDateSerializer.class).

It is working as expected.
Reply all
Reply to author
Forward
0 new messages