2

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

  • jsonString1: "{"originText" : "Original"}"
  • invoiceTest1 (originCode null)
  • invoiceTest2 (originCode: "")

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

Community
  • 1
  • 1
OscarSanhueza
  • 317
  • 2
  • 13
  • Indeed they only speak of serialization in jackson documentation. – OscarSanhueza Nov 04 '16 at 08:08
  • So, the only way to do this is to use the DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT property, but it seems this can be just change globally and doesn't work with the annotation in question. – OscarSanhueza Nov 04 '16 at 08:23

1 Answers1

1

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;
  }
}
Community
  • 1
  • 1
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • This did not solve my problem, please check the edit in the question. I modified the class as you suggested, result is the same. And it is not Spring the problem as same is happening if I make the deserialization manually. – OscarSanhueza Nov 04 '16 at 07:59