Serializing a request for a JSON PATCH with Jackson

2.3k Views Asked by At

I'm using Java Spring Boot as a gateway to an API with a PATCH endpoint that uses JSON Patch. Is it possible to use Jackson to serialize the JSON Patch document if there are different types? For example, if I want my JSON Patch document to have 3 operations where the values are of different type, is it possible for Jackson to serialize each operation in 3 different ways?

[
    {
        "op": "replace",
        "path": "/name",
        "value": "foo bar"
    },
    {
        "op": "replace",
        "path": "/tags",
        "value": [
           "done",
           "complete"
       ]
    },
    {
        "op": "replace",
        "path": "/age",
        "value": 25
     },
]

I'm currently using a @RequestBody annotation to deserialize the request I receive from my frontend application.

// Controller
@PatchMapping(
  path = "/images/{imageId}",
  consumes = MediaType.APPLICATION_JSON_VALUE,
  produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<ResponseEntity<String>> updateImage(
  @RequestBody @NotBlank List<UpdateOp> request) {
  return imageService.updateImage(request);
  }

// Image Service 
public Mono<ResponseEntity<String>> updateImage(List<UpdateOp> request) {
...
.body(BodyInserters.fromObject(objMapper.writeValueAsBytes(request)))
...
}

I'm a Spring Boot noob so open to suggestions and alternate solutions.

0

There are 0 best solutions below