-2

I am trying to return RemoveHoldPayload in my API. I noticed that when I used the following getter name isToBeClosedResult(1) then the response is missing toBeClosedResult but when I change the getter name to a standard name : getToBeClosedResult then the variable is getting returned in the response object JSON.

Why is this happening ?

(1) : --------------

{
    "autoHolds": null,
    "manualHolds": null,
    "incidentId": null,
    "comment": null,
    "commentType": null,
    "error": null,
    "toBeClosed": false,
    "closeStatusCode": 0,
    "commentId": null
}

API: -------------

@PostMapping(value = "/dummyAPI")
public Object dummyAPI () throws Exception {
    RemoveHoldPayload rh = new RemoveHoldPayload();
    return rh;
}

CLASS : -----------

import java.util.List;

public class RemoveHoldPayload {
    
    private List<AggregatedHoldListResource> autoHolds, manualHolds;
    private String incidentId, comment, commentType, toBeClosedResult,error;
    private boolean toBeClosed;
    private short closeStatusCode;
    private Integer commentId;
    
    public String getToBeClosedResult() {
        return toBeClosedResult;
    }
    public void setToBeClosedResult(String toBeClosedResult) {
        this.toBeClosedResult = toBeClosedResult;
    }
    
    public String getIncidentId() {
        return incidentId;
    }
    public void setIncidentId(String incidentId) {
        this.incidentId = incidentId;
    }
    public boolean getToBeClosed() {
        return toBeClosed;
    }
    public void setToBeClosed(boolean toBeClosed) {
        this.toBeClosed = toBeClosed;
    }
    public short getCloseStatusCode() {
        return closeStatusCode;
    }
    public void setCloseStatusCode(short closeStatusCode) {
        this.closeStatusCode = closeStatusCode;
    }
    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }
    public String getError() {
        return error;
    }
    public void setError(String error) {
        this.error = error;
    }
    public Integer getCommentId() {
        return commentId;
    }
    public void setCommentId(Integer commentId) {
        this.commentId = commentId;
    }
    public String getCommentType() {
        return commentType;
    }
    public void setCommentType(String commentType) {
        this.commentType = commentType;
    }
    public List<AggregatedHoldListResource> getAutoHolds() {
        return autoHolds;
    }
    public void setAutoHolds(List<AggregatedHoldListResource> autoHolds) {
        this.autoHolds = autoHolds;
    }
    public List<AggregatedHoldListResource> getManualHolds() {
        return manualHolds;
    }
    public void setManualHolds(List<AggregatedHoldListResource> manualHolds) {
        this.manualHolds = manualHolds;
    }
}
test dummy
  • 81
  • 4
  • 11

1 Answers1

2

Spring use Jackson as a standard JSON library and till the end, it doesn't know what is the actual type of your variable. So basically it uses Java Code Conventions where all getters start with get

Check this link: Naming convention for getters/setters in Java

You can use @JsonAlias which defines one or more alternative names for a property to be accepted during deserialization i.e. setting JSON data to Java object.

Or @JsonProperty to define logical property. @JsonProperty can be annotated at non-static setter or getter method or non-static object field. The logical property is used in serialization and de-serialization of JSON. @JsonProperty is annotated as following.

For example, you can do smth like that:

@JsonProperty("toBeClosed") 
public Boolean isToBeClosedResult() {
   return toBeClosed;
}
Antaaaa
  • 233
  • 3
  • 14