[play-java 2.4] Custom ObjectMapper for JSON

282 views
Skip to first unread message

raunak

unread,
Jul 28, 2015, 8:08:09 AM7/28/15
to play-framework
In order to integrate a custom ObjectMapper (as defined https://www.playframework.com/documentation/2.4.x/JavaJsonActions#Advanced-usage), the old way was to configure it in Global.onStart(). As far as I can tell, the new way is to inject the custom ObjectMapper as a dependency. 

Does that mean that I create a new java class, something like this..

public class CustomObjectMapper{
  public CustomObjectMapper() {
    ObjectMapper mapper = new ObjectMapper()
        // enable features and customize the object mapper here ...
        .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
        .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        // etc.
    Json.setObjectMapper(mapper);
  }
}

The part I have trouble understanding is, how will this get injected? Where would I use the @inject syntax, if that is the correct method of injecting it.

Michael Dop

unread,
Jul 28, 2015, 8:59:33 AM7/28/15
to play-framework, rauna...@gmail.com

raunak

unread,
Jul 28, 2015, 9:02:06 AM7/28/15
to play-framework, michae...@gmail.com
Awesome. I was on the right path, but wasn't sure if that was the way to go. 

Thanks for your input nonetheless. 
Reply all
Reply to author
Forward
0 new messages