java - Disable Jersey's ObjectMapper caching? -
i use jersey 2.9 , jackson 2.5.1 generate json responses on restful api.
in order provide custom objectmapper
, implement contextresolver
so:
@provider public class objectmapperprovider implements contextresolver<objectmapper> { @context uriinfo uriinfo; private final objectmapper objectmapper; public objectmapperprovider() { objectmapper = new objectmapper(); objectmapper.disable(deserializationfeature.fail_on_unknown_properties); objectmapper.enable(serializationfeature.indent_output); objectmapper.enable(serializationfeature.order_map_entries_by_keys); } @override public objectmapper getcontext(class<?> type) { // todo: configure custom object mapper depending on query string params return objectmapper; } }
it works great first time access endpoint: getcontext
method invoked , perform custom configurations on objectmapper (possibly returning new one) depending on query string provided user.
however, once endpoint has been accessed once, jersey seems cache object mapper , never ask again invoking getcontext
. need disable or prevent behavior since query string might have changed , might need return different object mapper request.
is possible??
thank you!!
note object mapper relatively expensive create. thread safe , designed reused. instead of creating own context can consider customizing the jackson jax-rs provider per request without creating new mapper instance.
there couple ways can achieve that.
you can annotate resource method the jackson specific annotations. example:
@jacksonfeatures(serializationenable = { serializationfeature.order_map_entries_by_keys }) public void foo() {}
but doesn't allow pass request parameters object mapper.
another option create instance of objectwritermodifier/objectreadermodifier , register via thread local injection via objectwriterinjector/objectreaderinjector. see example.
you can try combine per-request scope context provider , thread local modification.
Comments
Post a Comment