【问题标题】:How to clean fields by ObjectMapper?如何通过 ObjectMapper 清理字段?
【发布时间】:2021-07-01 17:05:53
【问题描述】:

我需要找到删除项目的解决方案

@RestController
@PatchMapping
public User fieldUpdate(Long userId, String field) {
    User updatedUser = userServiceImplement.remove(Long userId, String field);
    return updatedUser;
}

【问题讨论】:

  • 谁能给点建议?
  • mapper.readForUpdating(user) 这是错误的。 ObjectMapper 没有公开这样的方法。请更正您发布的代码并添加您的方法readForUpdating(user) 以便能够有意义
  • mapper.readForUpdating(user) 是正确的。这是杰克逊
  • 我仍然没有清除字段的解决方案,可能需要更改 ObjectMapper 设置..
  • 有人可以帮忙吗?

标签: java spring-restcontroller objectmapper


【解决方案1】:

您似乎忘记调用 repository.save(User)。

【讨论】:

  • 注意到了,但我只是在这里忘记了这个字符串)
  • “updateUser”对象在保存之前是否真的更新了?
  • 是的,它更新得很好,但我的目标是清理。
  • 您确定“用户”和“更新用户”的 ID 相同吗?可能它执行插入而不是更新
  • 看起来很好奇。可以发一下SQL日志吗?您写道,在 ObjectMapper 映射后,您的 updateUser 具有来自 JSON 请求的正确值。例如,如果请求包含“name: null”,则 UpdateUser 必须将“name”字段设为 null;你的情况是这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-15
  • 2015-01-24
  • 2018-01-07
  • 2011-04-10
  • 2017-05-08
  • 2015-07-07
  • 2019-04-10
相关资源
最近更新 更多