Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Spring ResponseEntityExceptionHandler不解析覆盖消息_Spring Mvc_Spring Boot - Fatal编程技术网

Spring mvc Spring ResponseEntityExceptionHandler不解析覆盖消息

Spring mvc Spring ResponseEntityExceptionHandler不解析覆盖消息,spring-mvc,spring-boot,Spring Mvc,Spring Boot,我通过扩展ResponseEntityExceptionHandler覆盖BindException 我在属性包中提供了自定义消息。但是,ResponseEntityExceptionHandler无法识别它 它确实解决了控制器请求参数上的TypeMistMatch。 但它不在ResponseEntityExceptionHandler中 我错过什么了吗 @Component @ControllerAdvice public class ExceptionHandler extends Resp

我通过扩展ResponseEntityExceptionHandler覆盖BindException

我在属性包中提供了自定义消息。但是,ResponseEntityExceptionHandler无法识别它

它确实解决了控制器请求参数上的TypeMistMatch。 但它不在ResponseEntityExceptionHandler中

我错过什么了吗

@Component
@ControllerAdvice
public class ExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleBindException(final org.springframework.validation.BindException ex,
            final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
        logger.info(ex.getClass().getName());
        //
        final List<String> errors = new ArrayList<String>();
        for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
            errors.add(error.getField() + ": " + error.getDefaultMessage());
        }
        for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
            errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
        }
        final ErrorMessage apiError = new ErrorMessage(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors.toString());
        return new ResponseEntity<>(apiError, new HttpHeaders(), HttpStatus.BAD_REQUEST);
    }

}

我也遵循了那个教程,这个错误不在这个类中。它在课堂上。您应该生成getter和setter,这在教程中没有显示


教程链接:

您需要在ErrorMessage类中定义getter。

在我的@ControllerAdvice中包含了一个选项“@Order(Ordered.HIGHEST_priority)”,解决了我的问题,它与您的问题相同


您的
@Override
带注释的方法
HandleBindeException
不应该也应该用
@ExceptionHandler(MethodArgumentNotValidException.class)注释吗?请在此处共享该答案的要点,而不仅仅是链接到外部资源
typeMismatch.demoPojo.instant= Supposed to be a date
typeMismatch.instant=Instant field
typeMismatch.java.time.Instant=Instant type