Home > Net >  Spring WebClient partial update with @PATCH method when calling another microservice
Spring WebClient partial update with @PATCH method when calling another microservice

Time:08-01

I have Node.js and Java services where I am trying to use the PATCH method and update the single item in the request body. When I am sending the below request body directly to the Node.js service(http://localhost:8004/products/xxx) it just updates the size and the rest of the items did not change.

{
    "size": 42,
}

But when I hit my Java service(http://localhost:8005/products/xxx) which calls the Node.js service it updates the size but the rest of the items are set to null like below. What I am doing wrong?

Updated product {
  title: null,
  desc: null,
  img: null,
  categories: null,
  size: '42',
  price: null
}

Java

public Mono<Products> updateProduct(String id, Products editProduct){
    return  webClient
            .patch()
            .uri("/products/{id}", id)
            .body(Mono.just(editProduct), Products.class)
            .retrieve()
            .bodyToMono(Products.class);
}

@PatchMapping("/products/{id}")
@ResponseStatus(code = HttpStatus.OK)
public Mono<Products> updateProduct(@PathVariable("id") String id, @Validated @RequestBody Products editProduct){
    return  basketService.updateProduct(id, editProduct);
}

Node.js

    router.patch('/:id', async (req, res) => {
      try{
        const productId = req.params.id
        const updates = req.body
    
        console.log("Updated product", updates)
    
        const result = await Product.findByIdAndUpdate(productId, updates)
        res.status(200).json(result);
      
      }catch(err) {
        res.status(500).json(err);
      }
})

CodePudding user response:

I think you can annotate your Products class to let Jackson know that it should not serialize null values:

@JsonInclude(Include.NON_NULL)
class Products {

 //...
}

You can also do that with a configuration property in Spring Boot globally, but this would affect also JSON payloads server by your application.

  • Related