From abb26e80a5617424d960a0f7d0b98fcb379a5224 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 21 Sep 2022 21:47:45 -0700 Subject: Fix forwarding date format --- src/main/java/org/traccar/MainModule.java | 9 +++-- .../helper/ObjectMapperContextResolver.java | 38 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/traccar/helper/ObjectMapperContextResolver.java (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index e0617a734..94669915b 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -62,6 +62,7 @@ import org.traccar.geolocation.UnwiredGeolocationProvider; import org.traccar.handler.GeocoderHandler; import org.traccar.handler.GeolocationHandler; import org.traccar.handler.SpeedLimitHandler; +import org.traccar.helper.ObjectMapperContextResolver; import org.traccar.helper.SanitizerModule; import org.traccar.mail.LogMailManager; import org.traccar.mail.MailManager; @@ -81,7 +82,6 @@ import javax.annotation.Nullable; import javax.inject.Singleton; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.ext.ContextResolver; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; @@ -110,14 +110,13 @@ public class MainModule extends AbstractModule { objectMapper.registerModule(new SanitizerModule()); } objectMapper.registerModule(new JSR353Module()); - objectMapper.setConfig(objectMapper - .getSerializationConfig().without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)); + objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); return objectMapper; } @Provides - public static Client provideClient(ObjectMapper objectMapper) { - return ClientBuilder.newClient().register((ContextResolver) clazz -> objectMapper); + public static Client provideClient(ObjectMapperContextResolver objectMapperContextResolver) { + return ClientBuilder.newClient().register(objectMapperContextResolver); } @Singleton diff --git a/src/main/java/org/traccar/helper/ObjectMapperContextResolver.java b/src/main/java/org/traccar/helper/ObjectMapperContextResolver.java new file mode 100644 index 000000000..b40e30d76 --- /dev/null +++ b/src/main/java/org/traccar/helper/ObjectMapperContextResolver.java @@ -0,0 +1,38 @@ +/* + * Copyright 2022 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.helper; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import javax.inject.Inject; +import javax.ws.rs.ext.ContextResolver; + +// This does not work as a lambda +public class ObjectMapperContextResolver implements ContextResolver { + + private final ObjectMapper objectMapper; + + @Inject + public ObjectMapperContextResolver(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + @Override + public ObjectMapper getContext(Class clazz) { + return objectMapper; + } + +} -- cgit v1.2.3