How should I inject custom ObjectMapper using compile-time dependency injection in Java?

77 views
Skip to first unread message

David Pinn

unread,
Sep 8, 2017, 2:19:02 AM9/8/17
to Play Framework
I have a BuiltInComponents implementation like this:

import play.core.ObjectMapperComponents;
...
final class MyComponents extends BuiltInComponentsFromContext

       
implements HttpFiltersComponents, AssetsComponents,
       
AhcWSComponents, EhCacheComponents, ObjectMapperComponents {
...


   
@Override
   
public ObjectMapper objectMapper() {
       
ObjectMapper mapper = Json.newDefaultMapper()
               
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
               
.registerModule(new Jdk8Module());
       
Json.setObjectMapper(mapper);
        applicationLifecycle
().addStopHook(() -> {
           
Json.setObjectMapper(null);
           
return CompletableFuture.completedFuture(null);
       
});
       
return mapper;
   
}


...
}

The compiler is yelling at me: "MyComponents is not abstract and does not override abstract method applicationLifecycle() in play.core.ObjectMapperComponents". 

I believe that's because applicationLifecycle() in ObjectMapperComponents returns play.api.inject.ApplicationLifecycle; but in BuiltInComponentsFromContext the applicationLifecycle() method returns play.inject.ApplicationLifecycle. What to do?

Am I going about setting up the custom ObjectMapper in the right way?

Greg Methvin

unread,
Sep 8, 2017, 4:46:39 AM9/8/17
to play-framework
play.core.ObjectMapperComponents is actually scala api. You already have implemented the `objectMapper()` method so you don't really need ObjectMapperComponents anyway, do you?

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/5a44f770-f8c2-43e6-872c-7818003e0740%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Tech Lead - Play Framework

Reply all
Reply to author
Forward
0 new messages