java - Single variable update of an Entity using a PUT request -
i have user entity follows:
public class user implements serializable { @id @generatedvalue(strategy = generationtype.identity) @column(name = "id") private integer id; @column(name = "name") private string name; @column(name = "age") private integer age; @notnull @size(min = 5, max = 50) @pattern(regexp = regexconstants.email_regex, message = validationmessages.email_validation_message) @column(name = "email", unique = true) private string email; @notnull @size(min = 5, max = 50) @column(name = "password") private string password; @notnull @size(min = 5, max = 50) @column(name = "country_code") private string countrycode; /* getters , setters */ }
i want update country code of user. want use put request seems appropriate choice (don't hate me using word appropriate here, might wrong) seems over-kill send user in request body not intend update country code. not have whole user when making request. user body (@requestbody user)
contains name
, country code
.
so, based on i've read put requests, need send complete user object in request not have. can accomplish passing country code in url articles have read on restful web services recommend against it.
current code:
@requestmapping(value = "/user/{id}", method = requestmethod.put) public responseentity<user> updateuser(@pathvariable("id") long id, @requestbody user user) { system.out.println("updating user " + id); user currentuser = userservice.findbyid(id); if (currentuser==null) { system.out.println("user id " + id + " not found"); return new responseentity<user>(httpstatus.not_found); } currentuser.setcountrycode(user.getcountrycode); userservice.updateuser(currentuser); return new responseentity<user>(currentuser, httpstatus.ok); }
i appreciate if can guide right way this. have read patch request not sure if way go in case. if doing wrong helpful if can provide example or give me starting point how take in correct format.
Comments
Post a Comment