How to "unset" injectableValues from objectMapper?

2.7k views Asked by At

i am using injectableValues to deserealize a json. I am doing this like:

    final InjectableValues.Std injectableValues = new InjectableValues.Std();
    injectableValues.addValue(HttpRestResponse.class, response);
    emailsResponse = this.prepareCustomMapper().reader(EmailsResponse.class).withInjectableValues(injectableValues)
        .readValue(response.getBody());

protected ObjectMapper prepareCustomMapper() {
    if (this.mapper == null) {
        this.mapper = new ObjectMapper();
        this.mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        this.mapper.setSerializationInclusion(Inclusion.NON_NULL);
    }
    return this.mapper;
}

So, this works fine. My problem is that when i don't want to inject values i do it like this:

EmailResponse emailResponse = this.prepareCustomMapper().readValue(response.getBody(), EmailResponse.class);

And this time, i am getting this exception:

java.lang.IllegalStateException: No 'injectableValues' configured, can not inject value with id [com.despegar.cross.commons.utils.restutils.HttpRestResponse]
    at org.codehaus.jackson.map.deser.StdDeserializationContext.findInjectableValue(StdDeserializationContext.java:101)
    at org.codehaus.jackson.map.deser.impl.PropertyValueBuffer.inject(PropertyValueBuffer.java:54)
    at org.codehaus.jackson.map.deser.impl.PropertyBasedCreator.startBuilding(PropertyBasedCreator.java:111)
    at org.codehaus.jackson.map.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:892)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:739)

this is my EmailResponse.class constructors:

// I use this one if i want to inject HttpRestResponseObject 
public EmailResponse(@JacksonInject final HttpRestResponse request) {
    super();
    this.request = request
}

public EmailResponse() {
    super();
}

So, my conclusion was that injectable value that i setted at first objectMapper's mapping , persists at second objectMapper's mapping, and it is trying to inject value to a constructor that no recieves it. I tried putting into prepareCustomMapper() this, so by this way i thought that it would "clean" injectedValues everytime that was called:

this.mapper.setInjectableValues(null);

But this didn't work neither.

Do you know how to "unset" injectableValues? or how to pass injectableValues for the current mapping that woldn't persists in the future. Thanks from now!

1

There are 1 answers

0
Ilya Ovesnov On BEST ANSWER

You can inject 'null' value:

final InjectableValues.Std injectableValues = new InjectableValues.Std();
    injectableValues.addValue(HttpRestResponse.class, null);
    emailsResponse = this.prepareCustomMapper().reader(EmailsResponse.class).withInjectableValues(injectableValues)
        .readValue(response.getBody());