Working on an API which is allowing the empty spaces being sent from the RequestHeader
of the request from POSTMAN which is not allowed but the request binding is successful and have tried with different ways of using and overriding methods in ResponseEntityExceptionHandler
. Can someone please suggest the solution,Have used all the constarint validations such as @Valid
,@NotBlank
,@NotEmpty
.
ControllerAdvice
@ControllerAdvice
public class DemoControllerAdvice extends ResponseEntityExceptionHandler{
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
// TODO Auto-generated method stub
return super.handleMethodArgumentNotValid(ex, headers, status, request);
}
@Override
protected ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
System.out.println(ex.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
}
Controller
@RestController
public class StudentResource {
@GetMapping(path="/testing")
public void method(@RequestBody Student student,@Valid
@RequestHeader(value="Authorization") @Size(min=1) @NotBlank String auth)
{
System.out.println("Valid request");
}
}
It should not be able to accept empty literal values like " " similar to the postman