1

LATER EDIT 2019-05-31

If I write a sample main method which instantiates an Item and then call String s = new ObjectMapper().writeValueAsString(item);, then the custom serializer is called correctly and has effect.

The issue only appears when the whole app is deployed in an Apache TomEE server.


LATER EDIT: it's not an issue with placement of annotation (on field vs. on getter), I tried various combinations of this (annotation on getter, annotation on private field, annotation on public field, etc...)


The code:

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.
    }

The ISSUES:

  • from what I know, the default JSON output format of the date should be the timestamp. In my case, it's not, instead it's yyyyMMddHHmmssZ
  • the custom serializer has no effect, I cannot change the output format of the date, and the serialize method never gets called.

The jackson files in my lib folder: jackson-annotations-2.8.0.jar, jackson-core-2.8.8.jar, jackson-databind-2.8.8.1.jar.

What am I doing wrong ?

Thank you.

Serban
  • 592
  • 1
  • 4
  • 24
  • "The issue only appears when the whole app is deployed in an Apache TomEE server." - perhaps you can debug your application to check which serialization method your application server is actually using. Your error sounds like TomEE implicitely does not use the "right" serialization method you are expecting by setting `Produces`. – Smutje Jun 05 '19 at 06:38
  • I tried debugging, my custom serializer is not called. All-in-all, after few days of tryouts and debugging and [more investigations](https://stackoverflow.com/questions/56396866/java-lang-classnotfoundexception-javax-ws-rs-ext-messagebodyreader-in-apache-to), I just gave up and used the built-in Johnzon JSON implementation from TomEE. I still don't know if it's possible and how it can be done to use Jackson instead of Johnzon... – Serban Jun 05 '19 at 07:19

1 Answers1

0

It might have something to do with your annotation being placed on the getter - you might move it to reflect something similar to

public class Item {

    @JsonSerialize(using = CSer.class)
    private Date lastModified;

   // ...
}

or you have to configure Jackson to only use getters for serialization.

Smutje
  • 17,733
  • 4
  • 24
  • 41
  • No, this isn't the cause. I tried various combinations (public field with annotations, annotations on getter, on field, etc...). – Serban May 29 '19 at 12:44