[英]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: 现在实现的方式是:
SELECT
all columns from the items
table SELECT
items
表中的所有列 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): 期望的响应(按期望顺序):
null
value is input, the column-value is simply not written to the database (and no error is produced) null
值,则不会直接将列值写入数据库(并且不会产生错误) You have two choices when working with the database: 使用数据库时,有两种选择:
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.