0

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");

}

}

Image where the Authorization headers is Empty

It should not be able to accept empty literal values like " " similar to the postman

  • While the duplicate question might not be exactly the same, in essence, it's the same problem. When using method argument vallidation, you should add the `@Validated` annotation to your controller. – g00glen00b Dec 05 '19 at 11:52
  • @g00glen00b,It did not work and please let me know the difference between Valid and Validated annotations –  Dec 05 '19 at 12:36
  • There's a Stack Overflow question about that as well: https://stackoverflow.com/questions/36173332/difference-between-valid-and-validated-in-spring. I'll add it to the list of duplicates. – g00glen00b Dec 05 '19 at 12:39

0 Answers0