【问题标题】:Spring boot custom exception not getting printed in logsSpring Boot自定义异常未在日志中打印
【发布时间】:2019-06-03 21:07:22
【问题描述】:

我正在尝试为我的 Spring boot REST 项目实现自定义异常。自定义异常被调用,但对错误消息的显示方式没有影响。

这是我用于自定义错误的 POJO:

public class ApiError {
    private HttpStatus status;
    private String message;
    private List<String> errors;

    public ApiError(HttpStatus status, String message, List<String> errors) {
        super();
        this.status = status;
        this.message = message;
        this.errors = errors;
    }

    public ApiError(HttpStatus status, String message, String error) {
        super();
        this.status = status;
        this.message = message;
        errors = Arrays.asList(error);
    }
}

这是我写的异常处理程序:

@ControllerAdvice
@EnableWebMvc
public class ApiExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(Exception.class)
    public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
        System.out.println("Custom exception!!");
        //List<String> details = new ArrayList<>();
        //details.add(ex.getLocalizedMessage());
        //System.out.println("Localize message:: "+ex.getLocalizedMessage());
        // ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(),

        //request.getDescription(false));
        ApiError error = new ApiError(HttpStatus.INTERNAL_SERVER_ERROR,"Server Error", request.getDescription(false));
        return new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

此外,我在控制器中定义了以下方法:

@RequestMapping(value = "/model", params = "number", method = RequestMethod.GET, produces = "application/json")
List<Model> getModel(HttpServletRequest request,@RequestParam(value = "codeNumber") String number) throws Exception{

    List<Model> model = null;
    try {
        model = niiService.getModel(number);
    }catch(RuntimeException e){
        new Exception(e);
    }
    return model;
}

但是,我看到的不是我的自定义 POJO,而是以下异常:

{
    "timestamp": 1547013989124,
    "status": 500,
    "error": "Internal Server Error",
    "message": "Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure\n\nThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.",
    "path": "/model"
}

我期待的是以下 JSON 结构:

{
    "status": 500,
    "message": "Server Error"
    ..
}

请告诉我,我缺少什么才能以我想要的方式获得错误响应。

【问题讨论】:

    标签: spring-boot error-handling custom-exceptions


    【解决方案1】:

    您在自定义异常前面缺少throw。 还要确保您在控制器中捕获了正确的异常。

    改变

    catch (RuntimeException e) {
       //custom exception
        new Exception(e);
    } 
    

    catch (RuntimeException e) {
        //custom exception
        throw new Exception(e);
    }
    

    【讨论】:

    • 感谢您的关注。但我仍然没有看到输出消息的方式有任何变化。
    【解决方案2】:

    我错过了向 APIError 类添加 getter 和 setter。因此,反应并没有像我预期的那样出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-12
      • 2019-12-10
      • 2017-03-15
      • 1970-01-01
      • 2021-07-10
      • 2016-04-07
      • 2020-02-25
      • 2014-12-16
      相关资源
      最近更新 更多