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.