Korzystając z poradnika https://www.leveluplunch.com/[...]ring-rest-webservice-request/ chcę dokonać walidacji formularza za pomocą java bez przeładowywania strony. Robię to w REST API.
Mój kontroler
@PutMapping(value = "/changeEmail", consumes = MediaType.APPLICATION_JSON_VALUE)
public HttpEntity<ChangeEmailDTO> showChangeEMail(
@RequestBody @Valid ChangeEmailDTO changeEmailDTO,
BindingResult result
) {
System.out.println("Email: " + changeEmailDTO.getEmail());
if(result.hasErrors()) {
System.out.println("Error: " + changeEmailDTO.getEmail());
return ResponseEntity.badRequest().body(changeEmailDTO);
}
System.out.println("Success: " + changeEmailDTO.getEmail());
return ResponseEntity.ok(changeEmailDTO);
}
przyjmuje adres email, waliduje w adnotacjach
@NotEmpty
@Getter @Setter private String email;
Według podanego wyżej poradnika, podobno powinno zwrócić error code w postaci json. Natomiast jedyny efekt jaki otrzymuje to https://zapodaj.net/81a6db4635168.png.html
Czyli zwraca mi sam obiekt, a nie tak jak np. w poradniku
{"timestamp":1417379464584,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MethodArgumentNotValidException","message":"Validation failed for argument at index 0 in method: public org.springframework.http.ResponseEntity<demo.AgencyResource> demo.AgencyController.saveAgency(demo.AgencyResource), with 2 error(s): [Field error in object 'agencyResource' on field 'name': rejected value [null]; codes [NotNull.agencyResource.name,NotNull.name,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [agencyResource.name,name]; arguments []; default message [name]]; default message [may not be null]] [Field error in object 'agencyResource' on field 'id': rejected value [50]; codes [Max.agencyResource.id,Max.id,Max.java.lang.Integer,Max]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [agencyResource.id,id]; arguments []; default message [id],20]; default message [must be less than or equal to 20]] ","path":"/agencies"}
W jaki sposób zwrócić takie dane?