If you check RFC 7231 Section 4.3.4 PUT, you'll see that
4.3.4. PUT
The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload.
In simple terms, PUT is when you either create a resource at url, or replace it completely (a file upload is an easy example).
In your case, an author is not entity in itself (I can see that its just a String
and the list is an @ElementCollection
). Which means, when you add an author, you're modifying the User
entity. i.e, you're not
creating or replacing the 'User'. Therefore, PUT is not appropriate for adding the author.
PUT is also not for deleting an entity. You should consider DELETE for that.
Now, RFC 7231 Section 4.3.4 POST says this:
4.3.3. POST
The POST method requests that the target resource process the
representation enclosed in the request according to the resource's
own specific semantics. For example, POST is used for the following
functions (among others):
- Providing a block of data, such as the fields entered into an HTML
form, to a data-handling process;
- Posting a message to a bulletin board, newsgroup, mailing list,
blog, or similar group of articles;
- Creating a new resource that has yet to be identified by the
origin server; and
- Appending data to a resource's existing representation(s)
Again, in simple words, POST can be a request to initiate an action at the server. The last two in the example list above applies to your case - you're appending or creating an author. Which means POST is appropriate for adding the author.
Conclusion:
Use POST and DELETE for adding and deleting authors.
On a side note, you should probably get rid of the add
and remove
at the end of the url. Let the HTTP Methods POST and DELETE differentiate the actions.
So it should be
@PostMapping("/api/profile/author")
@DeleteMapping("/api/profile/author")