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.
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/b877cc2c-6c2a-4349-81c7-6634766c968f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.