Issue
I am using path variable in url in order to update my object. How can I modify my code for it to work without needing to provide an id in the post body if I already got it in the url?
public class Person {
private String name;
private UUID id;
public(UUID id, String name) {
this.id = id;
this.name = name;
}
...getters
}
Service class
public int updatePerson(UUID id, Person person) {
String sql = "UPDATE person SET name = ? WHERE id = ?";
return jdbcTemplate.update(sql, person.getName(), person.getId());
}
Controller
@PutMapping("/{id}")
public int updatePerson(@PathVariable UUID id, @RequestBody Person person) {
return personService.updatePerson(id, person);
}
Solution
Just change your Service class to use id
instead of person.getId()
:
public int updatePerson(UUID id, Person person) {
String sql = "UPDATE person SET name = ? WHERE id = ?";
return jdbcTemplate.update(sql, person.getName(), id);
}
Answered By - João Dias