简体   繁体   English

在 spring 引导中处理异常的最佳选项/替代方法是什么?

[英]What's the best option/alternative to treat exceptions in spring boot?

Right now i'm using this example of exception handling:现在我正在使用这个异常处理示例:

//get an object of type curse by id
//in the service file, this findCurseById() method throws a 
//CursaNotFoundException

@GetMapping("/{id}")
public ResponseEntity<curse> getCursaById (@PathVariable("id") Long id) {

        curse c = curseService.findCurseById(id);
        return new ResponseEntity<>(c, HttpStatus.OK);

}

//so if not found, this will return the message of the error

@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(CursaNotFoundException.class)
public String noCursaFound(CursaNotFoundException ex) {
    return ex.getMessage();
}

and that's my exception那是我的例外

public class CursaNotFoundException extends RuntimeException {
    public CursaNotFoundException(String s) {
        super(s);

    }
}

in future I want to use Angular as front-end, so I don't really know how I should treat the exceptions in the back-end.将来我想使用 Angular 作为前端,所以我真的不知道应该如何处理后端的异常。 For this example let's say, should I redirect the page to a template.html page in the noCursaFound() method, or should I return something else?对于这个例子,我应该在noCursaFound()方法中将页面重定向到 template.html 页面,还是应该返回其他内容? A json or something? json 之类的? I couldn't find anything helpful.我找不到任何有用的东西。 Thanks谢谢

I would suggest keeping the error handling at the REST API level and not redirecting to another HTML page on the server side.我建议将错误处理保持在 REST API 级别,而不是重定向到服务器端的另一个 HTML 页面。 Angular client application consumes the API response and redirects to template.html if needed. Angular 客户端应用程序使用 API 响应并在需要时重定向到 template.html。

Also, it would be better if the backend returns an ApiError when an exception occurs with a message and, optionally, an error code:此外,如果后端在异常发生时返回一个ApiError消息和可选的错误代码会更好:

public class ApiError {
    private String message;
    private String code;
}

and handle the exceptions in a separate class, ExceptionHandler annotated with @ControllerAdvice :并在单独的 class 中处理异常,用@ControllerAdvice注释的ExceptionHandler

@ControllerAdvice
public class ExceptionHandler {
    @ExceptionHandler(value = CursaNotFoundException.class)
    public ResponseEntity cursaNotFoundException(CursaNotFoundException cursaNotFoundException) {
        ApiError error = new ApiError();
        error.setMessase(cursaNotFoundException.getMessage());
        error.setCode(cursaNotFoundException.getCode());
        return new ResponseEntity(error, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<> genericException(Exception exception) {
        ApiError error = new ApiError();
        error.setMessase(exception.getMessage());
        error.setCode("GENERIC_ERROR");
        return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM