Рассмотрим следующий объект: например:
Код: Выделить всё
@Entity
private class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/* let's assume the following attributes may be null */
private String firstName;
private String lastName;
/* getters and setters ... */
}
Код: Выделить всё
@Repository
public interface PersonRepository extends CrudRepository
{
}
Код: Выделить всё
private class PersonDTO {
private String firstName;
private String lastName;
/* getters and setters ... */
}
Код: Выделить всё
@RestController
@RequestMapping("/api/people")
public class PersonController {
@Autowired
private PersonRepository people;
@Transactional
@RequestMapping(path = "/{personId}", method = RequestMethod.PUT)
public ResponseEntity update(
@PathVariable String personId,
@RequestBody PersonDTO dto) {
// get the entity by ID
Person p = people.findOne(personId); // we assume it exists
// update ONLY entity attributes that have been defined
if(/* dto.getFirstName is defined */)
p.setFirstName = dto.getFirstName;
if(/* dto.getLastName is defined */)
p.setLastName = dto.getLastName;
return ResponseEntity.ok(p);
}
}
Код: Выделить всё
{"firstName": "John"}
Запрос с нулевым свойством
Код: Выделить всё
{"firstName": "John", "lastName": null}
Я не могу различить эти два случая, поскольку
Код: Выделить всё
lastNameПримечание:
Я знаю, что лучшие практики REST ( RFC 6902) рекомендуют использовать PATCH вместо PUT для частичных обновлений, но в моем конкретном сценарии мне нужно использовать PUT.
Подробнее здесь: https://stackoverflow.com/questions/384 ... dates-in-s
Мобильная версия