Java Integer null变为0

时间:2018-11-14 10:42:15

标签: java

在调用REST服务以保存Integer时,我注意到null值变成了0,但我不知道为什么会这样。这是我的课程

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CompanyPreference") public class CompanyPreference {
  public CompanyPreference() {
      super();
  }

  @XmlElement
  private Integer preferenceValueNumber;

  public void setPreferenceValueNumber(Integer preferenceValueNumber) {
      this.preferenceValueNumber = preferenceValueNumber;
  }

  public Integer getPreferenceValueNumber() {
      return preferenceValueNumber;
  }
}

这是我的网络服务

@POST
@SecureIt
@Consumes(value = { MediaType.APPLICATION_JSON })
@Produces(value = { MediaType.APPLICATION_JSON })
@Path("/{companyCode}/{companyType}/preferences/update")
@ApiOperation(value = "Update Company Preferences", response = DefaultResponse.class, authorizations = { @Authorization(value="Bearer") })
public Response savePreferences(@ApiParam(value = "Company Code", required = true) @PathParam("companyCode") String companyCode,
                                @ApiParam(value = "Company Type", required = true) @PathParam("companyType") String companyType,
                                @ApiParam(value = "Array of Company Preference objects", required = true) CompanyPreference[] companyPreferences,
                                @Context ContainerRequestContext crc) throws ResponseError {

  for(int i = 0; i < companyPreferences.length; i++) {
     System.out.println(companyPreferences[i].getPreferenceValueNumber());
  }

}

即使preferenceValueNumber是一个Integer而不是基本int,但当我的请求看起来像这样时,它仍然变成0 ...

[
  { preferenceValueNumber: "" },  //this returns 0
  { preferenceValueNumber: 30 }   //this returns 30 as it should
]

如何保留原始值null?

0 个答案:

没有答案