Reputation: 5926
I'm implementing a REST API and I want to use PUT method to create or update a resource. The resource is identified by an ID, which can be sent by user like this:
{
"id": 5,
"name": "John"
}
The Spring Boot Rest Controller accepts the messages and uses a Spring Data Repository, which has auto-generated IDs. If a record in DB with this ID exists, it will be updated, otherwise a new one with this ID will be created. However the IDs are created by Hibernate sequence. Should I move the Hibernate sequence if user adds resource with ID 5, but the Hibernate sequence is at number 2, or is there a better way how to handle this problem?
Upvotes: 0
Views: 1069
Reputation: 31
I suggest you to create a query method GET:
GET: /users/{userId}
This help you to get your ID.
Regards.
Upvotes: 1
Reputation: 1144
I believe not, you should separate POST/PUT API to create/update like
POST: /users
PUT: /users/{user_id}
body: {
"name": "John",
...
}
Upvotes: 2
Reputation: 36
In Hibernate, if you want to add new Entity with auto-generated ID, you SHOULD NOT pass them ID column,or if you already have entity with ID, you should set ID field to null.
Upvotes: 1