Last active
September 12, 2024 06:56
-
-
Save dschulten/4718b60b95bf9253dab8009febfa6d24 to your computer and use it in GitHub Desktop.
Set default timezone of jax-rs jackson provider for deserialization of timestamp to XmlGregorianCalendar JAXB date field.
This file contains hidden or 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
package de.kommone.kmc.api; | |
import java.util.TimeZone; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.ext.ContextResolver; | |
import javax.ws.rs.ext.Provider; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
@Provider | |
@Produces("application/json") | |
public class JacksonConfigurator implements ContextResolver<ObjectMapper> { | |
private ObjectMapper mapper = new ObjectMapper(); | |
public JacksonConfigurator() { | |
mapper.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); | |
} | |
@Override | |
public ObjectMapper getContext(Class<?> arg0) { | |
return mapper; | |
} | |
} |
This file contains hidden or 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
... | |
<dependency> | |
<groupId>org.glassfish.jersey.containers</groupId> | |
<artifactId>jersey-container-servlet</artifactId> | |
<version>2.25.1</version> | |
<exclusions> | |
<exclusion> | |
<groupId>org.glassfish.jersey.media</groupId> | |
<artifactId>jersey-media-jaxb</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.media</groupId> | |
<artifactId>jersey-media-json-jackson</artifactId> | |
<version>2.25.1</version> | |
</dependency> | |
... |
This file contains hidden or 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
<servlet> | |
<servlet-name>javax.ws.rs.core.Application</servlet-name> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>javax.ws.rs.core.Application</servlet-name> | |
<url-pattern>/api/*</url-pattern> | |
</servlet-mapping> |
Perhaps it is a classloading issue, at least that was what I was facing. I was using Payara5 which already provides Jackson libraries and my war contained them as well. Removing them from my war resolved it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Just doing that, the JacksonConfigurator constructor is called, but getContext() method is never called.
What could be a problem?