当前位置: 首页 > 知识库问答 >
问题:

异常处理程序在spring boot中不起作用

盖诚
2023-03-14

我正在用spring boot 2开发Rest API,我试图创建一个ExceptionHandler,但它似乎不起作用。

我有以下@GetMapping方法:

@GetMapping("/customers/{customerId}")
public Customer getCustomerById(@PathVariable Long customerId) {
    log.debug("### Enter: getCustomerById() for id: " + customerId);
    Optional<Customer> customer = customerRepository.findById(customerId);

    if (!customer.isPresent()) {
        throw new CustomerNotFoundException("The customer with id: " + customerId + " was not found!");
    }

    return customer.get();
}

customerRepository是扩展CrudRepository接口的接口。

customerNotFoundException的@ExceptionHandler如下所示:

@ExceptionHandler(CustomerNotFoundException.class)
public final ResponseEntity handleCustomerNotFoundException
        (CustomerNotFoundException customerNotFoundException, WebRequest webRequest) {
    log.error("### Oups! We have a CustomerNotFoundException!!!");
    ExceptionResponse exceptionResponse = new ExceptionResponse(
            new Date(),
            customerNotFoundException.getMessage(),
            webRequest.getDescription(false));

    return new ResponseEntity(customerNotFoundException, HttpStatus.NOT_FOUND);
}

我还对ResponseEntityExceptionHandler扩展类进行了如下注释:

@Slf4j
@RestController
@ControllerAdvice
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

问题是,当我调用数据库中不存在的customerId的请求时,我收到的不仅仅是CustomerNotFoundException消息,而是一个非常长的stacktrace:

{"cause":null,"stackTrace":[{"methodName":"getCustomerById","fileName":"CustomerResource.java","lineNumber":37,"className":"org.pdm.ib.pmt.router.controls.CustomerResource","nativeMethod":false},{"methodName":"invoke0","fileName":"NativeMethodAccessorImpl.java","lineNumber":-2,"className":"sun.reflect.NativeMethodAccessorImpl","nativeMethod":true},{"methodName":"invoke","fileName":"NativeMethodAccessorImpl.java","lineNumber":62,"className":"sun.reflect.NativeMethodAccessorImpl","nativeMethod":false},{"methodName":"invoke","fileName":"DelegatingMethodAccessorImpl.java","lineNumber":43,"className":"sun.reflect.DelegatingMethodAccessorImpl","nativeMethod":false},{"methodName":"invoke","fileName":"Method.java","lineNumber":498,"className":"java.lang.reflect.Method","nativeMethod":false},

等等...

问题出在哪里?

谢谢!

共有1个答案

施冠玉
2023-03-14

感谢@bestwishes,我找到了解决办法。这是回来的

new ResponseEntity(exceptionResponse, HttpStatus.NOT_FOUND)

而是返回

new ResponseEntity(customerNotFoundException, HttpStatus.NOT_FOUND)

在CustomResponseEntityExceptionHandler类的handleCustomerNotFoundException方法中。

 类似资料:
  • 我有一个带post请求的控制器。我试图用一个简单的NotNull注释验证POJO。我正在使用ControllerAdvice来处理异常。 所以我尝试使用它,但当我启动应用程序时,我得到了以下信息: 因此,我想为BindException创建自己的处理程序,但当我为BindException类创建ExceptionHandler时,spring应用程序不会启动。如果我注释掉handleBindExc

  • 我的RestController类: 我的ExceptionHandler: 例外响应:

  • 结果只是显示无穷大,当我尝试除以0时,它不打印行。

  • 我试图抓住无效的json,而解析它与jiffy在牛仔web套接字处理程序。如果json是有效的/无效的,我想转发一个适当的消息到,它将回复客户端。这是我的代码。 这会导致运行时异常。 12:07:48.406[错误]牧场侦听器http已连接到进程 那我该怎么做呢?

  • 1.1 异常处理的基本使用 try: <语句块1> except: <语句块2> try 捕获异常 except 发生异常时执行 try: <语句块1> except <异常类型名字>: <语句块2> except <异常类型名字> 发生对应异常时才会执行 1.2 异常处理的高级使用 try: <语句块1> except

  • 出于演示目的,我在生产环境中运行我的应用程序,而不是在开发环境中。 我在ValuesController中抛出错误。ValuesController如下所示: 我的如下所示: