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?