java - How do I get a reference to the Jackson Object Mapper in a jersey2 / hk2 application -


i have jersey2 application configured json support via jackson, adding

<dependency>     <groupid>org.glassfish.jersey.media</groupid>     <artifactid>jersey-media-json-jackson</artifactid>     <version>${jersey.version}</version> </dependency> 

in pom file ,

public myapplication() {     ...     register(jacksonfeature.class)     ... } 

in application. works, resources deserialized pojos arguments

@post @consumes(mediatype.application_json) public void blah(mypojo p) {     ... } 

now 1 of thoese resources needs reference jackson's objectmapper deserialization on own. i've tried doing like

@inject public myresource(@context objectmapper mapper) {     ... } 

or

@get public string foo(@context objectmapper mapper) {     ... } 

but in both cases reference mapper null. how can inject reference objectmapper in resources?

first there no default objectmapper used jackson provider. doesn't use objectmapper @ actually. makes use of other jackson apis handle (de)serialization.

if want use/inject single objectmapper instance, should create factory it

public class objectmapperfactory implements factory<objectmapper> {      final objectmapper mapper = new objectmapper();      @override     public objectmapper provide() {         return mapper;     }      @override     public void dispose(objectmapper t) {}    } 

then bind it

register(new abstractbinder(){     @override     public void configure() {         bindfactory(objectmapperfactory.class)             .to(objectmapper.class).in(singleton.class);     } }); 

one thing should noted configuration of objectmapper not thread safe. tried configure resource method, operations not thread safe.

another thing note jackson provider, if provide contextresolver, mentioned @laurentiu l, jackson provider switch using our objectmapper. in case, if want use same objectmapper, can in factory. example

public class objectmapperfactory implements factory<objectmapper> {      private final providers providers;     final objectmapper mapper = new objectmapper();      public objectmapperfactory(@context providers providers) {         this.providers = providers;     }      @override     public objectmapper provide() {         contextresolver<objectmapper> resolver = providers.getcontextresolver(                 objectmapper.class, mediatype.application_json);         if (resolver == null) { return mapper; }          return resolver.getcontext(null);     }      @override     public void dispose(objectmapper t) {}    } 

for above work (use single objectmapper), need make sure implement contextresolver<objectmapper>, , make sure annotation contextresolver corresponding @produces , @consumes media types.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -