Spring Restcontroller Requestbody DTO object property on integer is becoming zero

13 Views Asked by At

I have a JSON Object shown below which is passed as a Request Body to the Spring RestController. The RestController maps to its corresponding DTO and updated the table with new details. I see in the network tab, that value of id is passed with what ever the value UI sent.(say 52) When I put a debugger in Java , I see id is 0 in cityInfoDTO.

{
    "id" : 52
    "name": "State with counties",
    "counties": [
            "Allegany",
            "Bronx"
        ]
}

I have a JSON Object shown below which is passed as a Request Body to the Spring RestController. The RestController maps to its corresponding DTO and updated the table with new details. I see in the network tab, that value of id is passed with what ever the value UI sent.(say 52) When I put a debugger in Java , I see id is 0 in cityInfoDTO.

// Since we are updating, make sure that the id is valid and exists, so I added if (cityId.intValue() == cityInfoDTO.getId()) // cityInfoDTO.getId() is getting as 0. Not sure what is causing to set it as 0.

Controller.java

@PutMapping(value = "/updatecity/{id}", produces = "application/json") 
  public ResponseEntity<?> updateQuery(@PathVariable("id") Integer cityId, @RequestBody CityInfoDTO cityInfoDTO) {
    try {     
     
      if (cityId.intValue() == cityInfoDTO.getId()) {
        /// Sve
        return new ResponseEntity<>(HttpStatus.OK);
      } else {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
      }
    } catch (Exception ex) {
    }
  }

public CityInfoDTO {
 private int id;
 private String name;
 private List<String> counties;
 
   public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List<String> getCounties() {
    return counties;
  }

  public void setCounties(List<String>  counties) {
    this.counties = counties;
  }

}



component.ts

private updateState() {
         let stateInfo = 
{
    "id" : 52,
    "name": "State with counties",
    "counties": [
            "Allegany",
            "Bronx"
        ]
}
         this.networkServices.dataLoader.loadDataViaPut('/updatecity/52', stateInfo).subscribe({
          next: result => { 
          },
          error: error => {
          },
        });
}
0

There are 0 best solutions below