我试图在API中返回RemoveHoldPayload
。 我注意到,当我使用以下getter名称ISTOBeclosedResult
(1)时,响应将缺少TobeclosedResult
,但是当我将getter名称更改为标准名称:GetTOBeclosedResult
时,变量将在响应对象JSON中返回。
为什么会出现这种情况?
(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;
}
类:-----------
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;
}
}
spring使用作为标准的
JSON
库,直到最后,它都不知道变量的实际类型是什么。 所以基本上它使用Java代码约定,所有getter都以get
开头
查看此链接:Java中getters/setters的命名约定
您可以使用@jsonalias
,它为反序列化过程中要接受的属性定义一个或多个替代名称,即将JSON数据设置为Java对象。
或@jsonProperty
来定义逻辑属性。 @JSONProperty
可以在非静态setter或getter方法或非静态对象字段处进行注释。 逻辑属性用于JSON的序列化和反序列化。 @JSONProperty
注释如下。
例如,您可以这样做smth:
@JsonProperty("toBeClosed")
public Boolean isToBeClosedResult() {
return toBeClosed;
}