Skip to content

Instantly share code, notes, and snippets.

@AntonioMontanha
Created February 8, 2018 16:00
Show Gist options
  • Save AntonioMontanha/3b90b5c3b8ef0b6c31166cf1f840b3af to your computer and use it in GitHub Desktop.
Save AntonioMontanha/3b90b5c3b8ef0b6c31166cf1f840b3af to your computer and use it in GitHub Desktop.
@Autowired
private ViagemServices viagemService;
@PostMapping(path = "/new")
public ResponseEntity<Response<Viagem>> cadastrar(@Valid @RequestBody ViagemDto viagemDto, BindingResult result) {
Response<Viagem> response = new Response<Viagem>();
if (result.hasErrors()) {
result.getAllErrors().forEach(error -> response.getErrors().add(error.getDefaultMessage()));
return ResponseEntity.badRequest().body(response);
}
Viagem viagemSalva = this.viagemService.salvar(viagemDto);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(viagemDto.getId())
.toUri();
response.setData(viagemSalva);
return ResponseEntity.created(location).body(response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment