insert
Service
Antes
@Transactional
public UserDTO insert(UserDTO dto) {
User entity = new User();
copyDtoToEntity(dto, entity);
entity = repository.save(entity);
return new UserDTO(entity);
}
Depois
public Mono<UserDTO> insert(UserDTO dto) {
User entity = new User();
copyDtoToEntity(dto, entity);
return repository.save(entity).map(UserDTO::new);
}
Controller
Passamos um URIComponentsBuilder para inserir no created.
Antes
@PostMapping
public ResponseEntity<UserDTO> insert(@RequestBody UserDTO dto) {
dto = service.insert(dto);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(dto.getId()).toUri();
return ResponseEntity.created(uri).body(dto);
}
Depois
@PostMapping
public Mono<ResponseEntity<UserDTO>> insert(@RequestBody UserDTO userDTO,
UriComponentsBuilder builder) {
return service.insert(userDTO).map(userDTO1 ->
ResponseEntity.created(builder.path("/users/{id}").buildAndExpand(userDTO1.getId()).toUri())
.body(userDTO1));
}
Atualizado