Skip to content
Advertisement

Jackson conflicting setters, even with @JsonIgnore and @JsonProperty

I’m at a complete loss here. I have a class with overloaded setters for a property, and for the life of me cannot get Jackson to pick a correct setter. Stripping out the things not needed from the class, here’s the base of what I’ve got:

class TestDTO {
    
      @Setter(onMethod_={@JsonProperty})
      @JsonProperty
      protected CustomWrapper wrappedValues = new CustomWrapper();

      @JsonIgnore
      @XmlTransient
      public RecordAudit setWrappedValues(List<WrappedObject> wrappedValues) {
         this.customWrapper = new CustomWrapper(wrappedValues);
         return this;
      }

      @JsonIgnore
      @XmlTransient
      public RecordAudit setWrappedValues(CustomWrapper customWrapper) {
         this.customWrapper = customWrapper;
         return this;
      }
}

I have tried every combination I can think of of @JsonIgnore and @JsonProperty. I’ve tried just adding @JsonProperty to the @Setter annotation, I’ve tried only adding @JsonIgnore to the two custom setters, I’ve tried only @JsonProperty on the field itself, but no matter what I try, I get the following error:

Conflicting setter definitions for property "wrappedValues": ...#setWrappedValues(1 params) vs ...#setWrappedValues(1 params)

Does anyone have any ideas what’s going on here? Using Jackson 2.12.4, so I think just @JsonProperty should be all that’s needed, but as I mentioned above, that still results in the same error.

This is on JDK 11 if that makes a difference, I’m still new to 11, so am not sure how much that affects this.

Advertisement

Answer

You need to mark setter you want to use as com.fasterxml.jackson.annotation.JsonSetter.

class TestDTO {
    
      protected CustomWrapper wrappedValues = new CustomWrapper();

      public RecordAudit setWrappedValues(List<WrappedObject> wrappedValues) {
         this.customWrapper = new CustomWrapper(wrappedValues);
         return this;
      }

      @JsonSetter
      public RecordAudit setWrappedValues(CustomWrapper customWrapper) {
         this.customWrapper = customWrapper;
         return this;
      }
}

P.S. Your @Setter aren’t generating anything since there are methods with name setWrappedValues

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement