Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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
Java SpringBoot中的NoTouchElementException@ExceptionHandler不工作_Java_Spring_Exceptionhandler - Fatal编程技术网

Java SpringBoot中的NoTouchElementException@ExceptionHandler不工作

Java SpringBoot中的NoTouchElementException@ExceptionHandler不工作,java,spring,exceptionhandler,Java,Spring,Exceptionhandler,我在SpringBoot中创建了一个自定义异常处理程序 @RestControllerAdvice public class DataApiExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(NoSuchElementException.class) public final void **handleNoSuchElementException**(NoSuchElementE

我在SpringBoot中创建了一个自定义异常处理程序

@RestControllerAdvice
public class DataApiExceptionHandler extends ResponseEntityExceptionHandler {
 @ExceptionHandler(NoSuchElementException.class)
        public final void **handleNoSuchElementException**(NoSuchElementException ex) {
            System.err.println("This is throwing :"+ex.getMessage());
        }
...
@ExceptionHandler({ Exception.class })
    public ResponseEntity<Object> **handleAll**(final Exception ex) {
...
但是每次我抛出这个NosTouchElementException时,都会执行handleAll而不是handleNosChelementException

我可能错过了一些非常琐碎的事情。帮我弄清楚

***To change NoSuchElementException with NotFoundException does not make any difference.***

您似乎没有理解
@RestControllerAdvice
活动

注意:@RestControllerAdvice是在配置了适当的HandlerMappingHandlerAdapter对的情况下处理的,例如RequestMappingHandlerMappingRequestMappingHandlerAdapter对,这是MVC Java配置和MVC命名空间中的默认值

改用
@ControllerAdvice

您有一个
void
处理程序-为什么您希望得到响应

你在那里讲什么?应该是这样的:

@ControllerAdvice
public class InvalidValuesExceptionHandler extends ResponseEntityExceptionHandler {

  @ExceptionHandler({ InvalidValuesException.class })
  protected ResponseEntity<Object> handleInvalidRequest(RuntimeException exc, WebRequest request) {
    InvalidValuesException ive = (InvalidValuesException) exc;

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

    BasicResultMessage msg = new BasicResultMessage(ive.getDataId(),
                                                    ive.getMessage());
    SendDataResult result = new SendDataResult(false, msg);

    return handleExceptionInternal(exc, result, headers, HttpStatus.BAD_REQUEST, request);
  }
}
@ControllerAdvice
公共类InvalidValuesCeptionHandler扩展了ResponseEntityExceptionHandler{
@ExceptionHandler({InvalidValuesException.class})
受保护的响应handleInvalidRequest(运行时异常exc、WebRequest请求){
InvalidValuesException ive=(InvalidValuesException)exc;
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION\uJSON\uUTF8);
BasicResultMessage msg=新的BasicResultMessage(ive.getDataId(),
getMessage());
SendDataResult=新的SendDataResult(false,msg);
返回handleExceptionInternal(exc、结果、标题、HttpStatus.BAD_请求、请求);
}
}

你看过吗?没有,我现在正在看。谢谢任何想要具体答案的人,请转到仅删除(仅用于验证)handleAll**(),然后检查呼叫是否属于handleNoSchelementException**()你能简单地解释一下吗?我想你不明白这个问题。我没有要求回答。@Rishi move
public final void
with
protected ResponseEntity
这样做了……仍然在那里:(
@ControllerAdvice
public class InvalidValuesExceptionHandler extends ResponseEntityExceptionHandler {

  @ExceptionHandler({ InvalidValuesException.class })
  protected ResponseEntity<Object> handleInvalidRequest(RuntimeException exc, WebRequest request) {
    InvalidValuesException ive = (InvalidValuesException) exc;

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

    BasicResultMessage msg = new BasicResultMessage(ive.getDataId(),
                                                    ive.getMessage());
    SendDataResult result = new SendDataResult(false, msg);

    return handleExceptionInternal(exc, result, headers, HttpStatus.BAD_REQUEST, request);
  }
}