OscarSanhueza
OscarSanhueza

Reputation: 317

@JsonInclude not working on Deserialization with Spring, using mutable objects

I have found a lot about this, but no one having this same issue, the only that i can think is the last answer in this question where the mutability of the object makes the annotation work different.

So, I have an object like this

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(value = Include.NON_EMPTY)
public class Invoice implements Serializable {
  @JsonInclude(value = Include.NON_EMPTY)
  private String originCode;

  @JsonInclude(value = Include.NON_EMPTY)
  public String getOriginCode() {
    return originCode;
  }

  @JsonInclude(value = Include.NON_EMPTY)
  public void setOriginCode(String originCode) {
    this.originCode = originCode;
  }
}

When deserializing this object from a JSON, using spring framework the value of originCode keeps coming empty, if i use

{   
   "originCode" : ""
}

In the other way around if I use this object where originCode is already empty and I serialize it, the originCode is ignores in the serialized json.

Why this is working just when serializing?, how the fact that this object is mutable can affect in the use of this annotation when deserializing?


---EDIT---

The solution proposed here below did not fix the problem, I thought the problem was actually in spring. So I tried like this

@RequestMapping(method = RequestMethod.POST, value = "/test")
@ResponseBody
public ResponseEntity<InvMessage> testInvoice(
        @PathVariable(value = "someId") @Example({ "1233232-7" }) InvoicerId invoicerId,
        @RequestBody  Invoice2 invoiceRequest,
        InvMessage messageContainer)  {

    ObjectMapper mapper = new ObjectMapper();
    try {

        String jsonString1 = mapper.writeValueAsString(invoiceRequest);
        Invoice2 invoiceTest1 = mapper.readValue(jsonString1, Invoice2.class);
        String jsonInString2 = "{\"originCode\" : \"\",\"originText\" : \"Original\"}";
        Invoice2 testInvoice = mapper.readValue(jsonInString2, Invoice2.class);

    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ok(messageContainer);
}

So, when sending a request with body

{   
  "originCode" : "",
  "originText" : "Original"
}

The results are

Checking those results, i can see that always when deserializing that empty string I'm getting also an empty string inside the object, even I have defined the class like.

@JsonIgnoreProperties(ignoreUnknown = true)
public class Invoice2 implements Serializable {
  private static final long serialVersionUID = 1L;
  @JsonInclude(value = Include.NON_EMPTY)
  private String originCode; 
  private String originText; 
  public String getOriginCode() {
    return originCode;
  }
  public void setOriginCode(String originCode) {
    this.originCode = originCode;
  }
  public String getOriginText() {
     return originText;
  }
  public void setOriginText(String originText) {
    this.originText = originText;
  }
}

Jackson-databind version 2.5

Upvotes: 2

Views: 2126

Answers (1)

kuhajeyan
kuhajeyan

Reputation: 11027

Since you may be using Class level annotation and property level annotation, latter overriding the former explained here

Try,

@JsonIgnoreProperties(ignoreUnknown = true)
public class Invoice implements Serializable {
  @JsonInclude(value = Include.NON_EMPTY)
  private String originCode;


  public String getOriginCode() {
    return originCode;
  }


  public void setOriginCode(String originCode) {
    this.originCode = originCode;
  }
}

Upvotes: 1

Related Questions