Java 弹簧靴@ControllerAdvice/@有效
我正在编写Spring Boot中异常处理的示例演示应用程序。我正在尝试使用@ControllerAdvice处理异常 我想处理验证器引发的异常。它处理其他异常,但不处理MethodArgumentNotValidException 有关更多详细信息,请参见我正在学习的课程: Query.javaJava 弹簧靴@ControllerAdvice/@有效,java,spring-boot,validation,error-handling,controller-advice,Java,Spring Boot,Validation,Error Handling,Controller Advice,我正在编写Spring Boot中异常处理的示例演示应用程序。我正在尝试使用@ControllerAdvice处理异常 我想处理验证器引发的异常。它处理其他异常,但不处理MethodArgumentNotValidException 有关更多详细信息,请参见我正在学习的课程: Query.java @Getter @Setter @NoArgsConstructor @Validated public class Query implements Serializable{ @Size(
@Getter
@Setter
@NoArgsConstructor
@Validated
public class Query implements Serializable{
@Size(min = 7, max = 24, message = "Size must be between 7 and 24")
@Pattern(regexp = "[a-zA-Z0-9 ]+", Invalid characters")
private String number;
@Size(max = 2, message = "Size must be between 0 and 2")
@Pattern(regexp = "[a-zA-Z0-9 ]+", message="Invalid characters")
private String language;
}
@Setter
@Getter
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ErrorResponse
{
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
private LocalDateTime timestamp;
private HttpStatus status;
private int code;
private String error;
private String exception;
private String message;
private String path;
private List<String> errors;
}
ErrorResponse.java
@Getter
@Setter
@NoArgsConstructor
@Validated
public class Query implements Serializable{
@Size(min = 7, max = 24, message = "Size must be between 7 and 24")
@Pattern(regexp = "[a-zA-Z0-9 ]+", Invalid characters")
private String number;
@Size(max = 2, message = "Size must be between 0 and 2")
@Pattern(regexp = "[a-zA-Z0-9 ]+", message="Invalid characters")
private String language;
}
@Setter
@Getter
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ErrorResponse
{
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
private LocalDateTime timestamp;
private HttpStatus status;
private int code;
private String error;
private String exception;
private String message;
private String path;
private List<String> errors;
}
@Setter
@吸气剂
@诺尔格构装师
@JsonInclude(JsonInclude.Include.NON_NULL)
@资料
公共类错误响应
{
@JsonFormat(shape=JsonFormat.shape.STRING,pattern=“yyyy-MM-dd-hh:MM:ss”)
私有LocalDateTime时间戳;
私有http状态;
私有整数码;
私有字符串错误;
私有字符串异常;
私有字符串消息;
私有字符串路径;
私有列表错误;
}
CustomExceptionHandler.java
@SuppressWarnings({“unchecked”,“rawtypes”})
@控制器建议
@组件(“错误”)
公共类CustomExceptionHandler扩展了ResponseEntityExceptionHandler{
@ResponseStatus(未找到HttpStatus.NOT_)
@ExceptionHandler(NotFoundException.class)
公共最终响应属性handleNotFoundError(NotFoundException ex,最终HttpServletRequest请求){
ErrorResponse error=新的ErrorResponse();
error.setTimestamp(LocalDateTime.now());
错误。setMessage(例如getMessage());
错误.setCode(HttpStatus.NOT_FOUND.value());
返回新的ResponseEntity(错误,HttpStatus.NOT_FOUND);
}
@ResponseStatus(HttpStatus.INTERNAL\u SERVER\u错误)
@ExceptionHandler(InternalServerException.class)
公共最终响应HandleInternelServoreError(InternalServerException ex,最终HttpServletRequest请求){
ErrorResponse error=新的ErrorResponse();
error.setTimestamp(LocalDateTime.now());
错误。setMessage(例如getMessage());
error.setCode(HttpStatus.INTERNAL_SERVER_error.value());
返回新的响应属性(错误,HttpStatus.INTERNAL\u SERVER\u错误);
}
@ExceptionHandler(ConstraintViolationException.class)
public void constraintViolationException(HttpServletResponse响应)引发IOException{
response.sendError(HttpStatus.BAD_REQUEST.value());
}
@ResponseStatus(HttpStatus.BAD_请求)
@凌驾
受保护的ResponseEntity handleMethodArgumentNotValid无效(MethodArgumentNotValidException ex、HttpHeaders标头、HttpStatus状态、WebRequest请求){
List errorList=ex
.getBindingResult()
.getFieldErrors()
.stream()
.map(fieldError->fieldError.getDefaultMessage())
.collect(Collectors.toList());
ErrorResponse error=新的ErrorResponse();
错误.setCode(HttpStatus.BAD_REQUEST.value());
返回新的ResponseEntity(错误,HttpStatus.BAD_请求);
}
}
请求
public ResponseEntity<?> getData(HttpServletRequest httpServletRequest,
@Valid @ApiParam(value = "MANDATORY. The number") @PathVariable(value = "number", required = true) final String partNumber,
@Valid @ApiParam(value = "OPTIONAL. The language") @RequestParam(value = "language", required = false) final String languageKey) {
.............
}
public ResponseEntity getData(HttpServletRequest HttpServletRequest,
@有效@ApiParam(value=“MANDATORY.The number”)@PathVariable(value=“number”,required=true)最终字符串零件号,
@有效@ApiParam(value=“OPTIONAL.The language”)@RequestParam(value=“language”,required=false)最终字符串语言键){
.............
}
您创建了列表errorList
但从不使用它,返回空的ErrorResponse
尝试用@ExceptionHandler(MethodArgumentNotValidException.class)注释HandleMethodArgumentNotValidException方法,看看会发生什么?您没有处理此MethodArgumentNotValidException。。。