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.
On Friday, 31 May 2019 22:29:13 UTC, Serban Alexe wrote:
>
> 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/76428442-a41f-42d3-b80a-48fad4a78927%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.