-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NCL-6708] Provide a jackson instance loaded with timestamp module
This is used to properly serialize the Java 8 timestamps by Jackson. We use a similar thing in PNC-Orch
- Loading branch information
1 parent
fd8f9ce
commit ec057eb
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
reports-rest/src/main/java/org/jboss/da/rest/JacksonProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.jboss.da.rest; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.ext.ContextResolver; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
/** | ||
* Jackson Provider just to properly serialize the timestamp | ||
*/ | ||
@Provider | ||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_PATCH_JSON }) | ||
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_PATCH_JSON }) | ||
public class JacksonProvider implements ContextResolver<ObjectMapper> { | ||
private final ObjectMapper objectMapper; | ||
|
||
public JacksonProvider() { | ||
objectMapper = new ObjectMapper(); | ||
objectMapper.registerModule(new JavaTimeModule()); | ||
|
||
// write dates in ISO8601 format | ||
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | ||
} | ||
|
||
public ObjectMapper getMapper() { | ||
return objectMapper; | ||
} | ||
|
||
@Override | ||
public ObjectMapper getContext(Class<?> objectType) { | ||
return objectMapper; | ||
} | ||
} |