简体   繁体   English

Spring Restful编辑-保存更改的最佳实践是什么

[英]Spring Restful edit - what's the best practice to save changes

So what I am having is a spring controller: 所以我现在是一个弹簧控制器:

@Controller
@RequestMapping("/user")
public class UserController() {

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.PUT)
    public @ResponseBody HTTPResponseDTO edit(@RequestBody String data, @PathVariable long id) {
        // marshall data String to User entity
        ObjectMapper mapper = new ObjectMapper();
        User marshaledUser = mapper.readValue(data, User.class);
        User databaseUser = session.get(id, User.class);
        //hibernate things to save
        ...
        // session.save(user);
    }

The data string is in the following json format: 数据字符串采用以下json格式:

{"name":"value1",
 "email":"value2",
 "note":"value3"}

The responseDTO is something simple looks like: responseDTO看起来很简单:

{"result":"success"}

When the client is passing in a data json, it will only pass in the value that is being edited, eg: 当客户端传递数据json时,它将仅传递正在编辑的值,例如:

{"email":"value2"}

in this case, other non-supplied fields will be treated as null. 在这种情况下,其他未提供的字段将被视为null。

however "note" is a nullable field, and a user is allowed to clear the "note" field - in my current design, when it is doing so, it will be something like: 但是,“ note”是一个可为空的字段,并且允许用户清除“ note”字段-在我当前的设计中,这样做时,它将类似于:

{"note":null}

QUESTION 1: So here is my dilemma: how can I tell if a user is on purpose to set this to null, or it is null only because it is not passed in from the data string? 问题1:这是我的难题:如何判断用户是否故意将其设置为null,还是仅因为未从数据字符串中传入而将其设置为null?

QUESTION 2: When the controller is receiving the data String, I will have an entity coming from this data, I will then query database by id to get the existing User object. 问题2:当控制器接收到数据字符串时,我将有一个来自此数据的实体,然后将按ID查询数据库以获取现有的User对象。 what is the best practice to merge these two objects? 合并这两个对象的最佳实践是什么? Looking for what fields exist in the marshaledUser and setting those to the databaseUser looks like lots of repetitive jobs: 寻找在marshaledUser中存在的字段并将其设置为databaseUser看起来像很多重复的工作:

if (marshaledUser.name != null) {
    databaseUser.name = marshaledUser.name;
}
if (marshaledUser.email != null) {
    databaseUser.email = marshaledUser.email;
}
if (marshaledUser.note != null) { // refer to question 1...
    databaseUser.note = marshaledUser.note;
}

An object can easiler have over 5 fields - 5 if statements of almost same thing looks not worth it.. What is the best practice to handle this? 一个对象可以轻松拥有超过5个字段-如果几乎相同的语句看起来不值得,则为5个字段。处理此问题的最佳实践是什么?

QUESTION 1: So here is my dilemma: how can I tell if a user is on purpose to set this to null, or it is null only because it is not passed in from the data string? 问题1:这是我的难题:如何判断用户是否故意将其设置为null,还是仅因为未从数据字符串中传入而将其设置为null?

You can't. 你不能 So, either change your approach and always pass a complete User (that's what I would do), instead of only a few fields, or unmarshall the JSON to a Map (or a JsonObject). 因此,要么更改您的方法并始终传递一个完整的User(这就是我要做的),而不是仅传递几个字段,要么将JSON解组到Map(或JsonObject)。 If the key is present, but has a null value, then the value as been passed and is null. 如果密钥存在,但具有空值,则该值已传递且为空。 If the key is not present, then it hasn't been passed. 如果密钥不存在,则表示尚未通过。

QUESTION 2: When the controller is receiving the data String, I will have an entity coming from this data, I will then query database by id to get the existing User object. 问题2:当控制器接收到数据字符串时,我将有一个来自此数据的实体,然后将按ID查询数据库以获取现有的User对象。 what is the best practice to merge these two objects? 合并这两个对象的最佳实践是什么?

Same as question 1. It would be so much simpler if the client passed a complete User object instead of only a few fields. 与问题1相同。如果客户端传递了一个完整的User对象而不是仅传递几个字段,它将变得更加简单。 The method would simply look like 该方法看起来像

@RequestMapping(value = "/edit/{id}", method = RequestMethod.PUT)
public @ResponseBody HTTPResponseDTO edit(@RequestBody User user, @PathVariable long id)
    user.setId(id);
    session.merge(user);
}

Or at least, you would simply copy every field of the passed user to the persistent one, without caring if it has been sent or not. 或者至少,您只需将传递的用户的每个字段复制到持久字段,而不必关心是否已发送。

Also, returning a HttpResponseDTO containing result = success is redundant. 同样,返回包含result = success的HttpResponseDTO是多余的。 If the method completes normally, it will return with a 200 - OK HTTP status. 如果该方法正常完成,它将返回200-OK HTTP状态。 If it doesn't, then you should return another HTTP status code. 如果不是,那么您应该返回另一个HTTP状态代码。

I think 1 approach is to read your target object from the database and covert it to json. 我认为一种方法是从数据库中读取目标对象并将其隐式转换为json。 Then merge your request json using a technique described here 然后使用此处描述的技术合并您的请求json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将初始(批量)数据导入RESTful系统的最佳实践是什么? - What's the best practice for initial (bulk) data import into RESTful system? Spring/Spring 启动链式 bean 实例化的最佳实践是什么? - What's the best practice of Spring/Spring boot chained bean instantiation? Spring RESTFul:存储音频文件的最佳方式是什么? - Spring RESTFul: What's the best way to store audio files? 春季启动并行传出请求的最佳做法是什么? - What's the best practice for spring boot parallel outgoing requests? Spring Data JPA错误处理的最佳实践是什么 - What's best practice in Spring Data JPA Error handling 将 UUID 与 RESTful URL 标准一起使用的最佳做法是:/collection/{Id}? - What is the best practice to use UUID with RESTful URL standards: /collection/{Id}? 在 JAVA 中处理静态 Web 服务中的复杂对象的最佳实践是什么 - what is the best practice for handle complex object in a restful web services in JAVA Spring 引导中 JWT 的最佳实践是什么? - what is the best practice for JWT in Spring Boot? 打包结构化Spring项目的最佳实践是什么? - what is best practice to package structuring Spring project? 在 Spring 引导上创建存储库的最佳实践是什么? - What is the best practice to create repository on Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM