在spring-boot中发送的HTTP请求头中存在空格问题

7 浏览
0 Comments

在spring-boot中发送的HTTP请求头中存在空格问题

我正在处理一个API,该API允许从POSTMAN的请求的RequestHeader中发送空格。尽管不允许发送空格,但请求绑定成功,并尝试使用和重写ResponseEntityExceptionHandler中的不同方法。请问有人能给出解决方案吗?已经使用了所有的约束验证,如@Valid@NotBlank@NotEmpty

ControllerAdvice

 @ControllerAdvice
  public class DemoControllerAdvice extends ResponseEntityExceptionHandler{
@Override
protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
    // TODO Auto-generated method stub
    return super.handleMethodArgumentNotValid(ex, headers, status, request);
}
@Override
protected ResponseEntity 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

它不应该接受空的字面值,如" ",类似于Postman。

0