在我的Spring Boot 服务中,我使用https://github.com/java-json-tools/json-patch来处理PATCH请求。
一切似乎都很好,除了一种方法,以避免修改不可变的字段,如对象id的,创建时间等,我发现了一个类似的question在Github https://github.com/java-json-tools/json-patch/issues/21,我找不到正确的例子。
这个blog似乎给予了一些有趣的解决方案,关于使用node.js中的解决方案验证JSON补丁请求。如果知道JAVA中是否已经有类似的解决方案,那就太好了。
3条答案
按热度按时间368yc8dk1#
在很多情况下,你可以只修补一个只有用户可以写入的字段的中间对象,然后你可以很容易地使用一些对象Map器或手动地将中间对象Map到你的实体。
这样做的缺点是,如果您要求字段必须显式地为空,那么您将不知道补丁对象是否显式地将字段设置为空,或者它是否从未出现在补丁中。
您也可以为此滥用可选选项,例如:
尽管Optional不是这样使用的,但它是在维护输入类型的同时实现修补操作的最直接的方法。当可选字段为null时,它从未从客户端传递。当可选字段不存在时,这意味着客户端已将值设置为null。
l0oc07j22#
不要直接从客户端接收
JsonPatch
,而是定义一个DTO来处理验证,然后将DTO示例转换为JsonPatch
。假设您要更新示例
User.class
的用户,则可以定义如下DTO:自定义
toString
方法确保不使用null
值预先填充更新请求中不包含的字段。您的
PATCH
请求可以如下所示(为了简单起见,我没有考虑例外)以下方法返回上面在控制器中更新的已修补用户。
nzk0hqpo3#
另一个解决方案是强制反序列化并验证请求主体。
因此,示例DTO可能如下所示:
你的控制器可以是这样的:
不需要Optional's,没有额外的依赖,你的正常验证就可以了,你的昂首阔步看起来不错。唯一的问题是,你没有在嵌套对象上得到正确的合并补丁,但在许多用例中,这甚至是不需要的。