簡體   English   中英

Spring-Boot - 錯誤處理

[英]Spring-Boot - Error Handling

我正在嘗試在Spring-Boot中為我的控制器編寫錯誤處理程序,以便捕獲大多數可能的錯誤(Spring,sql等)。 到目前為止,我能夠通過Null獲得JSON響應,但是我無法將任何數據放入其中。 當我嘗試收到錯誤消息時,我只收到一個空白頁面。

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

@RestController
public class BasicErrorController implements ErrorController {
    private static final String ERROR_PATH = "/error";

    @RequestMapping(value=ERROR_PATH)
    @ExceptionHandler(value = {NoSuchRequestHandlingMethodException.class, SQLException.class, IOException.class, RuntimeException.class, Exception.class})
    public ErrorBody defaultErrorHandler(HttpServletRequest request, Exception e) {     
        ErrorBody eBody = new ErrorBody();         
        eBody.setMessage(e.getCause().getMessage());
        return eBody;
    }
}
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ErrorBody {
    private String dateTime;
    private String exception;
    private String url;
    private String message;
}

喲可以這樣做:

 @ControllerAdvice
   public class ControllerExceptionTranslator {


    @ExceptionHandler(EntityNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    SimpleErrorMessage handleException(EntityNotFoundException exception){
        log.debug("Entity Not Found Exception {}",exception.getMessage());
        log.trace(exception.getMessage(),exception);
        return new SimpleErrorMessage("Entity not found","This resource was not found");
    }


    @ExceptionHandler({UsernameNotFoundException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    SimpleErrorMessage handleException(UsernameNotFoundException exception){
        log.debug("Username not found {}",exception.getLocalizedMessage());
        log.trace(exception.getMessage(),exception);
        return new SimpleErrorMessage("Unaouthorized"," ");
    }


}

通過使用“HttpServletRequest請求”並從請求中讀取信息,我能夠獲得有關錯誤的數據並將它們作為json正確發送。

@RequestMapping(value = ERROR_PATH)
    public ErrorBody defaultErrorHandler(HttpServletRequest request) {....}

這是@ExceptionHandler(Exception.class)的一個例子

https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

你可以使用@ControllerAdvice

package demo.controller;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

@ControllerAdvice
public class ExceptionControllerAdvice {

 @InitBinder
 public void initBinder(WebDataBinder binder) {
    System.out.println("controller advice: init binder");
}


@ExceptionHandler(Exception.class)
public String exception(Exception e) {
    System.out.println("controller advice: exception Handler");
    System.out.println(e.getMessage());
    return "error";
}

@ModelAttribute
public void modelAttribute(){
    System.out.println("controller advice:model Attribute");
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM