0

I have User class like this :

@Data
@Entity
public class User {
@Id @GeneratedValue Long userID;
String eMail;
String passwordHash;
}

And a method in controller class to add value like this :

@PutMapping(path="/user/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
User replaceUser(@RequestBody User newUser, @PathVariable Long id) {

    return repository.findById(id)
            .map(employee -> {
                employee.setEMail(newUser.getEMail());
                employee.setPasswordHash(newUser.getPasswordHash());
                return repository.save(employee);
            })
            .orElseGet(() -> {
                newUser.setUserID(id);
                return repository.save(newUser);
            });
}

To insert value I use curl -X PUT -H "Content-Type: application/json" -d "{ \"email\": \"asd\", \"passwordHash\": \"sad\" }" http://localhost:8080/user/3

Is there any way I can make it little better like assigning 3 would be automatic?

Iterating whole .json won't be a good job, that's what I know.

Should I use Random method?

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • I don't understand what you're asking. Could you clarify? – JB Nizet Jul 28 '19 at 16:59
  • 1
    See [REST – PUT vs POST](https://restfulapi.net/rest-put-vs-post/). Basically, if you want the server to assign the id of the new object, you need to use `POST`. Only use `PUT` for *creating* objects if the client assigns the new "id". An example of using `PUT` for creating is when the object is named (file name). You should of course continue to use `PUT` for *updating*. – Andreas Jul 28 '19 at 17:03
  • How are you going to replace a user if don't know his ID? – mentallurg Jul 28 '19 at 17:04
  • 1
    @Andreas though this question has a relation with PUT Vs POST doubt. The OP has a question on taking user id for creating new users. I think you should not close this or mark as duplicate rather, create a reference. – fiveelements Jul 28 '19 at 17:07
  • @fiveelements OP is asking if there is a way for the id (3) to be automatically assigned. The answer is that you remove the id from the URL, and use `POST` to *create* the object. The response will then include the auto-generated id of the new object. The question is only a question because of the lack of understanding of how `POST` vs `PUT` works in REST. – Andreas Jul 28 '19 at 17:10
  • @Andreas can't agree completely. His main question is around generating user id. PUT Vs POST apart this doubt should be clarified and answered. Please do not mark as a duplicate of generic reference when the question has some specificity. – fiveelements Jul 28 '19 at 17:13
  • Anyway, @mentallurg A couple of things: 1. The method replaceUser() handles both create a user and update user functionality. This is currently done in PUT request. You may use PUT for updating user and create another method that handles POST for creating new users. For updating user take user id in the URI. 2. For new users, the user id (usually) neither should come from the client nor be a random number. The random number generated may have duplicates. Create and use a sequence in the database or auto-incremented number facility of some databases. – fiveelements Jul 28 '19 at 17:16
  • @fiveelements OP has already added the `@GeneratedValue` to have the database auto-generate the id if not supplied. What is needed is for OP to not supply the id value, by using `POST`. – Andreas Jul 28 '19 at 17:16
  • @Andreas: Why have you marked the question as duplicate? The question is NOT about PUT vs POST. The question question is about how can on identify resources, to use ID or to use other attributes. If you really understood the question, remove your mark that it is a duplicate. – mentallurg Jul 28 '19 at 17:20
  • @mentallurg I think you're the one misunderstanding the question, which is: "Is there a way to auto-generate the id?" --- Since OP already added `@GeneratedValue`, the answer is to *not* give the id in the request, i.e. to use `POST` instead of `PUT` when creating the object. – Andreas Jul 28 '19 at 17:22
  • Thanks, I should study before I post ... It was too easy, that's the problem ... – Maifee Ul Asad Jul 29 '19 at 04:56

0 Answers0