简体   繁体   English

HTTP PUT使用部分Json更新数据库-Spring,Postgres

[英]HTTP PUT to update database using partial Json - Spring, Postgres

I am writing a web api using Spring and Postgres. 我正在使用Spring和Postgres编写Web API。

  • I have a case where I take a Json object item 我有一个我拿Json对象item

  • The uri is /api/item/{itemId} uri是/api/item/{itemId}

  • Request type is PUT 请求类型为PUT

Json: 杰森:

{
   "name":"itemname",
   "description":"item description here"
}

So I do (using JdbcTemplate) a SELECT statement to check if the itemId exists and then update if it does. 所以我做(使用JdbcTemplate)一条SELECT语句来检查itemId存在,然后更新它是否存在。

I would also like to implement a case with partial puts taking Json that look like this:: 我还想用Json的部分推销来实现一个案例,如下所示:

{
   "name":"itemname"
}

OR 要么

{
   "description":"item description here"
}

where only the respective fields are updated. 仅更新各个字段。 In Spring, the variables not present are automatically null . 在Spring中,不存在的变量自动为null

The way this is implemented now is: 现在实现的方式是:

  1. SELECT all columns from the items table SELECT items表中的所有列
  2. Sequentially check every single expected variable for null and if they are null, replace the null with the value selected from the table in step 1. 依次检查每个预期变量是否为null,如果它们为null,则用在步骤1中从表中选择的值替换null。
  3. UPDATE all columns with the values (none of which should be null if the table has a not null constraint) 使用值UPDATE所有列(如果表具有not null约束,则任何列都不应该为null

Question: How do you do this without == null or != null checks? 问题:如何在没有== null or != null检查的情况下执行此操作? Is seems to be poor design and involves iterating through every single expected variable for every single PUT request (I will have many of those). 似乎设计不佳,涉及为每个单个PUT请求遍历每个单个预期变量(我将有很多这样的请求)。

Desired responses (in order of desirability): 期望的响应(按期望顺序):

  1. There's a way in Postgres where if a null value is input, the column-value is simply not written to the database (and no error is produced) 在Postgres中有一种方法,如果输入null值,则不会直接将列值写入数据库(并且不会产生错误)
  2. There is a way to use Spring (and Jackson) to create a Java object with only the provided values and a way to generate SQL and JdbcTemplate code that only updates those specific columns. 有一种方法可以使用Spring(和Jackson)使用仅提供的值来创建Java对象,并且可以生成仅更新这些特定列的SQL和JdbcTemplate代码。
  3. Patch is the way of life - implement it 补丁是一种生活方式-实施它
  4. Change the front-end to always send everything 更改前端以始终发送所有内容

You have two choices when working with the database: 使用数据库时,有两种选择:

  1. Just update what has changed, doing everything by yourself. 只需更新已更改的内容,即可自己完成所有操作。
  2. Get Jackson and Hibernate to do it for you. 让Jackson和Hibernate帮您做。

So let's look at No. 1: Let's say you're looking right now at the contents of an html form that has been sent back to the server. 因此,让我们看一下No. 1:假设您现在正在查看已发送回服务器的html form的内容。 Take every field in the html form and update only those fields in the database using an SQL statement. 使用html表单中的每个字段,并使用SQL语句仅更新数据库中的那些字段。 Anything that is not in the form will not get updated in your database table. 不在表格中的任何内容都不会在您的数据库表中得到更新。 Simple! 简单! Works well. 效果很好。 You don't need to worry about anything that is not in the form. 您无需担心表格中没有的任何内容。 You can restrict your update to the form's contents. 您可以将更新限制为表单的内容。

In general this is a simple option, apart from one problem, which is html checkboxes . 通常,这是一个简单的选项,除了一个问题,即html checkboxes If you are doing an update, html checkboxes can catch you out because due to a little design quirk, they don't get sent back to the server if they are unchecked. 如果您要进行更新,则html复选框可能会吸引您,因为由于一些设计上的怪癖,如果未选中它们,它们不会被发送回服务器。

No. 2: Perhaps you're looking for Hibernate, which you didn't mention. No. 2:也许您正在寻找未提及的Hibernate。 Jackson will fill a json object for you (must have a record id). Jackson将为您填充一个json对象(必须具有记录ID)。 Use Hibernate to populate a java class with the existing record, update with the new values Jackson has provided, then you tell Hibernate to merge() it into the existing record in the database, which it will. 使用Hibernate用现有记录填充Java类,用Jackson提供的新值进行更新,然后告诉Hibernate将其merge()合并到数据库中的现有记录中。 I also use Roo to create my Hibernate-ready classes for me. 我还使用Roo为我创建我的Hibernate就绪类。

No. 2 is hard to learn and set up, but once you've done sussed it, it's very easy to change things, add fields and so on. No. 2很难学习和设置,但是一旦您怀疑了它,就可以很容易地进行更改,添加字段等操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM