0

I am trying to implement a PATCH API as follows:

    @PatchMapping("/student)
    public ResponseEntity<StudentDTO> patchStudent(@RequestBody StudentDTO studentDTO)
            throws URISyntaxException {
            ...
    }

Here StudentDTO is as follows:

class StudentDTO {
   String name,
   String rollNum,
   String grade,
   String id,
  ...

}

Here in PATCH API, user may send any number of fields including id as follows:

Request 1:

{
  id: 1,
  name: "Test"
}

Request 2:

{
  id:1,
  name: "Test",
  rollNumber : "123456"
}

Request 3:

{
  id:1,
  name: "Test",
  rollNumber : "123456",
  grade : null.   //NOTE: user may send null as well for a field in request body
}

I a not getting how should I capture only those fields in the request body while patching the data in the backend?

Joy
  • 4,197
  • 14
  • 61
  • 131
  • What is the impact of `null` then ? `null` is also a valid value – Eklavya Jun 15 '20 at 06:09
  • 1
    Yeah @Eklavya, I mean someone may want to deliberately nullify one field. – Joy Jun 15 '20 at 06:25
  • @Joy I believe you can choose among a list of alternative solutions provided at https://stackoverflow.com/questions/38424383/how-to-distinguish-between-null-and-not-provided-values-for-partial-updates-in-s – feanor07 Jun 15 '20 at 10:30

0 Answers0