1

i'm creating CreateOrUpdateProduct API use Spring boot. i want to return to consumer two fields ('message & isOk'). But when i exec this API, i received ('message & ok') fields. what's happened? please expand me. thanks advance!

this is my function

public ResponseBase CreateOrUpdateProduct(Product product) {

        ....
        return responseBase;
    }
public class ResponseBase {
boolean isOk;
public boolean isOk() {
    return isOk;
}
public void setOk(boolean isOk) {
    this.isOk = isOk;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}
String message;
}

i received

{
  "message":null,
  "ok": true
}
Le Nguyen
  • 203
  • 3
  • 8

2 Answers2

1

I think your answer is here: Jackson renames boolean field by removing is

Jackson (serializer) sees "isOk" as a get method of a boolean variable named "ok". This is a common naming pattern developers use on get methods for boolean variables.

EDIT:

You shouldn't set the name of your method to "getIsOk", because that doesn't follow the naming convention of get method for boolean variables. This is not a very good solution, but it'll work.

Jackson provides an annotation to you that set the name of the serialized variable:

@JsonProperty(value="isOk")
public boolean isOk() {
  return isOk;
}
Álex Fogaça
  • 84
  • 1
  • 5
1

You should rename your getter to getIsOk().

It will return the expected answer :

{
  "message":null,
  "isOk": true
}
obourgain
  • 8,856
  • 6
  • 42
  • 57