Skip to content

Commit

Permalink
Externalize object mapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Apr 22, 2019
1 parent 9bf999e commit 3c818b7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -226,6 +229,8 @@ public boolean isRunning()
public void buildServer()
{



for (Class<?> controllerClass : registeredControllers) {
HandlerGenerator generator = new HandlerGenerator("io.sinistral.proteus.controllers.handlers", controllerClass);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends JacksonModule> clazz = (Class<? extends JacksonModule>) 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
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/io/sinistral/proteus/modules/ConfigModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public ConfigModule()
}
}



public ConfigModule(String configFile)
{
this.configFile = configFile;
Expand All @@ -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)
{
Expand Down
32 changes: 32 additions & 0 deletions core/src/main/java/io/sinistral/proteus/modules/JacksonModule.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c818b7

Please sign in to comment.