gpt4 book ai didi

spring - 未调用 ResponseEntityExceptionHandler 的重写 handleMethodArgumentNotValid 方法

转载 作者:行者123 更新时间:2023-12-04 11:47:59 26 4
gpt4 key购买 nike

我正在尝试为我的 Spring Boot Rest 服务设置一个自定义验证器和一个 ExceptionHandler,当我添加 ExceptionHandler 时,验证错误没有发送到 UI。所以我试图覆盖 handleMethodArgumentNotValid 方法,但这也不起作用。有人可以对此有所了解吗?

这就是我在 Controller 中配置验证类的方式 -

package services.rest.controller;

import java.io.IOException;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import services.rest.model.TestInput;
import services.rest.validator.DataValidator;

@RestController
@RequestMapping("/test")
@Slf4j
public class RestResource {

@Autowired
private DataValidator validator;

@PostMapping("/create")
public String create(@Valid final TestInput input) throws IOException {
log.debug(input.toString());
return "Success";
}

@InitBinder()
public void init(final WebDataBinder binder) {
binder.addValidators(validator);
}

}

这是我的 ExceptionHandler 代码 -
package services.rest.exceptionhandler;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@SuppressWarnings({ "unchecked", "rawtypes" })
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllExceptions(final Exception ex, final WebRequest request) {
System.out.println("All exceptions Method getting executed!!!!");

final List<String> details = new ArrayList<>();
details.add(ex.getLocalizedMessage());
return new ResponseEntity("Server Error", HttpStatus.INTERNAL_SERVER_ERROR);
}

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex,
final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
System.out.println("Validation Error Method getting executed!!!!");
final List<String> details = new ArrayList<>();
for (final ObjectError error : ex.getBindingResult().getAllErrors()) {
details.add(error.getDefaultMessage());
}
return new ResponseEntity("Validation Error", HttpStatus.BAD_REQUEST);
}
}

最初没有覆盖“handleMethodArgumentNotValid”方法。现在也覆盖它后,它不起作用

最佳答案

您是否检查了堆栈跟踪,可能会引发 ConstraintViolation 异常而不是 MethodArgumentNotValid 异常。 Spring 没有为此提供任何默认处理程序。

关于spring - 未调用 ResponseEntityExceptionHandler 的重写 handleMethodArgumentNotValid 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57188350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com