Handling Partial Updates with Java Records in Spring WebFlux

26 views Asked by At

I'm working on a Spring WebFlux project and facing a challenge with handling partial updates for my resources.

I want to be able to update only the fields that are provided in a JSON request, where fields can be explicitly set to null (to update the value in the database to null) or omitted (to leave the current value unchanged in the db).

I prefer to avoid using Optional in DTOs due to its problems in serialization/deserialization contexts, and I also want to avoid using maps due to type safety concerns.

I am considering using Java records as DTOs to ensure type safety and data immutability, but I'm struggling with differentiating between null values (meaning the client wants to update the field to null) and omitted fields (meaning the client wants the field to remain unchanged).

This is an example of the record I'm using: public record ItemUpdateRequest(String title, Long category, BigDecimal price) {} In this scenario, if title is null, it should update the title to null in the database. However, if title is not present in the request at all, the current title should remain unchanged.

Is there a way to customize the deserialization process to handle this scenario, or perhaps another approach I haven't considered?

Thanks in advance, I appreciate any advice that can help.

0

There are 0 answers