From 3c818b72fd54caa9bd4e5db64bfbb44cfda19966 Mon Sep 17 00:00:00 2001 From: Joshua Bauer Date: Mon, 22 Apr 2019 11:52:36 -0700 Subject: [PATCH] Externalize object mapper. --- .../sinistral/proteus/ProteusApplication.java | 5 +++ .../proteus/modules/ApplicationModule.java | 38 +++++++++++-------- .../proteus/modules/ConfigModule.java | 12 ++++++ .../proteus/modules/JacksonModule.java | 32 ++++++++++++++++ core/src/main/resources/reference.conf | 2 + core/src/test/resources/application.conf | 2 + 6 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 core/src/main/java/io/sinistral/proteus/modules/JacksonModule.java diff --git a/core/src/main/java/io/sinistral/proteus/ProteusApplication.java b/core/src/main/java/io/sinistral/proteus/ProteusApplication.java index d50afbe..ed1595a 100644 --- a/core/src/main/java/io/sinistral/proteus/ProteusApplication.java +++ b/core/src/main/java/io/sinistral/proteus/ProteusApplication.java @@ -14,6 +14,9 @@ import com.google.inject.name.Named; import com.typesafe.config.Config; import io.sinistral.proteus.modules.ConfigModule; +import io.sinistral.proteus.modules.JacksonModule; +import io.sinistral.proteus.server.Extractors; +import io.sinistral.proteus.server.ServerResponse; import io.sinistral.proteus.server.endpoints.EndpointInfo; import io.sinistral.proteus.server.handlers.HandlerGenerator; import io.sinistral.proteus.server.handlers.ServerDefaultHttpHandler; @@ -226,6 +229,8 @@ public boolean isRunning() public void buildServer() { + + for (Class controllerClass : registeredControllers) { HandlerGenerator generator = new HandlerGenerator("io.sinistral.proteus.controllers.handlers", controllerClass); diff --git a/core/src/main/java/io/sinistral/proteus/modules/ApplicationModule.java b/core/src/main/java/io/sinistral/proteus/modules/ApplicationModule.java index 16b57ec..a3ece92 100644 --- a/core/src/main/java/io/sinistral/proteus/modules/ApplicationModule.java +++ b/core/src/main/java/io/sinistral/proteus/modules/ApplicationModule.java @@ -59,21 +59,29 @@ public void bindMappers() this.bind(XmlMapper.class).toInstance(xmlMapper); - ObjectMapper objectMapper = new ObjectMapper(); - - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true); - objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); - objectMapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true); - objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); - objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true); - objectMapper.registerModule(new AfterburnerModule()); - objectMapper.registerModule(new Jdk8Module()); - - this.bind(ObjectMapper.class).toInstance(objectMapper); - this.requestStaticInjection(Extractors.class); - this.requestStaticInjection(ServerResponse.class); - } + try { + + String className = config.getString("application.jacksonModule"); + + log.info("Installing JacksonModule " + className); + + Class clazz = (Class) Class.forName(className); + + JacksonModule module = clazz.newInstance(); + + install(module); + + } catch (Exception e) { + + this.binder().addError(e); + + log.error(e.getMessage(), e); + + install(new JacksonModule()); + + } + + } @SuppressWarnings("unchecked") @Override diff --git a/core/src/main/java/io/sinistral/proteus/modules/ConfigModule.java b/core/src/main/java/io/sinistral/proteus/modules/ConfigModule.java index 09a5a26..0d1fd73 100644 --- a/core/src/main/java/io/sinistral/proteus/modules/ConfigModule.java +++ b/core/src/main/java/io/sinistral/proteus/modules/ConfigModule.java @@ -46,6 +46,8 @@ public ConfigModule() } } + + public ConfigModule(String configFile) { this.configFile = configFile; @@ -56,6 +58,16 @@ public ConfigModule(URL configURL) this.configURL = configURL; } + public Config getConfig() + { + return config; + } + + public void setConfig(Config config) + { + this.config = config; + } + @SuppressWarnings("unchecked") private void bindConfig(final Config config) { diff --git a/core/src/main/java/io/sinistral/proteus/modules/JacksonModule.java b/core/src/main/java/io/sinistral/proteus/modules/JacksonModule.java new file mode 100644 index 0000000..fa5a7f0 --- /dev/null +++ b/core/src/main/java/io/sinistral/proteus/modules/JacksonModule.java @@ -0,0 +1,32 @@ +package io.sinistral.proteus.modules; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.module.afterburner.AfterburnerModule; +import com.google.inject.AbstractModule; +import io.sinistral.proteus.server.Extractors; +import io.sinistral.proteus.server.ServerResponse; + +public class JacksonModule extends AbstractModule +{ + @Override + protected void configure() + { + ObjectMapper objectMapper = new ObjectMapper(); + + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true); + objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); + objectMapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true); + objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true); + objectMapper.registerModule(new AfterburnerModule()); + objectMapper.registerModule(new Jdk8Module()); + + this.bind(ObjectMapper.class).toInstance(objectMapper); + + this.requestStaticInjection(Extractors.class); + this.requestStaticInjection(ServerResponse.class); + } +} diff --git a/core/src/main/resources/reference.conf b/core/src/main/resources/reference.conf index 3afcb62..796bf00 100644 --- a/core/src/main/resources/reference.conf +++ b/core/src/main/resources/reference.conf @@ -22,6 +22,8 @@ application { defaultResponseListener = "io.sinistral.proteus.server.handlers.ServerDefaultResponseListener" + jacksonModule = "io.sinistral.proteus.modules.JacksonModule" + tmpdir = ${java.io.tmpdir}/${application.name} # path to default favicon file diff --git a/core/src/test/resources/application.conf b/core/src/test/resources/application.conf index 9c55400..ddd47d4 100644 --- a/core/src/test/resources/application.conf +++ b/core/src/test/resources/application.conf @@ -22,6 +22,8 @@ application { defaultResponseListener = "io.sinistral.proteus.server.handlers.ServerDefaultResponseListener" + jacksonModule = "io.sinistral.proteus.modules.JacksonModule" + tmpdir = ${java.io.tmpdir}/${application.name} # path to default favicon file