简体   繁体   中英

can't response 'isOk' field in spring boot

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
}

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;
}

You should rename your getter to getIsOk() .

It will return the expected answer :

{
  "message":null,
  "isOk": true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM