How to distinguish between null and not provided values for partial updates in Spring Rest Controller

后端 未结 7 840
囚心锁ツ
囚心锁ツ 2020-12-04 17:32

I\'m trying to distinguish between null values and not provided values when partially updating an entity with PUT request method in Spring Rest Controller.

Consider

7条回答
  •  半阙折子戏
    2020-12-04 18:10

    Actually,if ignore the validation,you can solve your problem like this.

       public class BusDto {
           private Map changedAttrs = new HashMap<>();
    
           /* getter and setter */
       }
    
    • First, write a super class for your dto,like BusDto.
    • Second, change your dto to extend the super class, and change the dto's set method,to put the attribute name and value to the changedAttrs(beacause the spring would invoke the set when the attribute has value no matter null or not null).
    • Third,traversal the map.

提交回复
热议问题