Java spring boot中HTTP请求标头中发送空白的问题
正在处理一个API,该API允许从邮递员发出的请求的Java spring boot中HTTP请求标头中发送空白的问题,java,rest,spring-boot,http,request-headers,Java,Rest,Spring Boot,Http,Request Headers,正在处理一个API,该API允许从邮递员发出的请求的RequestHeader发送空格,这是不允许的,但请求绑定成功,并尝试了ResponseEntityExceptionHandler中使用和重写方法的不同方法。有人能推荐解决方案吗?他们使用了所有的常量验证,例如@Valid,@NotBlank,@NotEmpty 控制器建议 @ControllerAdvice public class DemoControllerAdvice extends ResponseEntityExcepti
RequestHeader
发送空格,这是不允许的,但请求绑定成功,并尝试了ResponseEntityExceptionHandler
中使用和重写方法的不同方法。有人能推荐解决方案吗?他们使用了所有的常量验证,例如@Valid
,@NotBlank
,@NotEmpty
控制器建议
@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();
}
}
它应该不能接受类似于邮递员的“”之类的空文本值,而重复的问题可能并不完全相同,本质上是相同的问题。当使用方法参数vallidation时,您应该将
@Validated
注释添加到控制器中。@g00glen00b,它不起作用,请让我知道有效注释和已验证注释之间的区别。关于这一点,还有一个堆栈溢出问题:。我将把它添加到副本列表中。
@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");
}
}