Home > database >  PUT request wont go through unless I provide ID in body
PUT request wont go through unless I provide ID in body

Time:11-29

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);
    }

CodePudding user response:

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);
}

CodePudding user response:

If I understand your question, you're looking to use the Person as a payload template for updating an instance of that class.

You can utilise @JsonProperty annotations.

public class Person {
    
    private String name;

    @JsonProperty(access = Access.WRITE_ONLY)
    private UUID id;
    
    public(UUID id, String name) {
        this.id = id;
        this.name = name;
    }
    ...getters
} 

CodePudding user response:

Ideally you will use the path variable to locate the information stored in your database. Consider for instance:

@PostMapping("/{id}")
public ResponseEntity<int> updatePerson(@PathVariable UUID id, @RequestBody Person person) {
  Person alreadyExistingPerson = personService.getPersonById(id);
  if (alreadyExistingPerson == null) {
    return ResponseEntity.notFound().build();
  }

  // Then, update the information you already have for Person with the new provided
  // See this related SO question for options:
  // https://stackoverflow.com/questions/65367995/copy-changed-fields-into-an-object-in-java/65369332#65369332
  // In a simple use case
  alreadyExistingPerson.setName(person.getName());

  return personService.updatePerson(alreadyExistingPerson);
}

The question linked in the code.

Your service will include:

public int getPersonById(UUID id) {
  String sql = "SELECT id, name FROM person WHERE id = ?";
  // The queryForObject method has a lot of overloads, use the one you consider appropriate
  return jdbcTemplate.queryForObject(sql, new Object[]{id}, (rs, rowNum) ->
    new Person(
      UUID.fromString(rs.getString("id")),
      rs.getString("name")
    )
  );
}

public int updatePerson(Person person) {
  String sql = "UPDATE person SET name = ? WHERE id = ?";
  return jdbcTemplate.update(sql, person.getName(), person.getId());
}

As you are updating information, it is preferable to use a POST HTTP method rather than PUT.

  • Related