![](/img/trans.png)
[英]Why doesn't my service default exception handler catch my service exceptions?
[英]Springboot exception handler doesn't catch exception
在Spring Boot and Rest應用程序中,我已按如下方式配置了異常處理程序。 如果在請求使其停止服務后引發異常,則該方法工作正常。
Rest api期望Content-Type為“ application / json”,並且如果我不將該內容標頭發送到api,則異常處理程序將無法捕獲該異常。 它在日志中打印以下信息:
DEBUG [o.s.w.s.DispatcherServlet] DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/app/v1.0/customers/customer/zones/zoneName.]
DEBUG [o.s.w.s.m.m.a.RequestMappingHandlerMapping] Looking up handler method for path /app/v1.0/customers/customer/zones/zoneName.
DEBUG [o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver] Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain' not supported
DEBUG [o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver] Invoking @ExceptionHandler method: public final org.springframework.http.ResponseEntity<java.lang.Object> org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest)
DEBUG [o.s.w.s.DispatcherServlet] Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
DEBUG [o.s.w.s.DispatcherServlet] Successfully completed request
這是異常處理程序類:
@ControllerAdvice
@RestController
public class ServiceExceptionHandler extends ResponseEntityExceptionHandler
{
@ExceptionHandler(Exception.class)
public final ResponseEntity<java.lang.Object> handleException(Throwable ex,WebRequest req)
{
ErrorResponse errorResponse = null;
if (ex instanceof ApiException)
{
errorResponse = new ErrorResponse((ApiException) ex);
}
else
{
logger.error(ex);
errorResponse = new ErrorResponse();
errorResponse.setCode(HttpCodes.HTTP_CODE_500);
errorResponse.setError(ex.getMessage());
}
return new ResponseEntity<Object>(errorResponse,
HttpStatus.valueOf(errorResponse.getCode()));
}
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex,
HttpHeaders headers, HttpStatus status, WebRequest request)
{
Map<String, String> responseBody = new HashMap<String, String>();
responseBody.put("path", request.getContextPath());
responseBody.put("message",
"The URL you have reached is not in service at this time (404).");
return new ResponseEntity<Object>(responseBody, HttpStatus.NOT_FOUND);
}
}
您的課程將僅捕獲從控制器內部引發的錯誤。 您看到的錯誤發生在調用控制器之前,因為Spring無法找到控制器來處理請求。
DEBUG [o.s.w.s.m.m.a.RequestMappingHandlerMapping] Looking up handler method for path /app/v1.0/customers/customer/zones/zoneName.
DEBUG [o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver] Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain' not supported
請注意,處理程序為null。
您還需要實現HandlerExceptionResolver
來處理這種類型的異常: http : HandlerExceptionResolver
有關更多詳細信息,請參見控制器外部的Spring異常處理程序 。
這是最終的解決方案:
@ControllerAdvice
public class SpringExceptionHandler extends ExceptionHandlerExceptionResolver
{
@ExceptionHandler(org.springframework.web.HttpMediaTypeNotSupportedException.class)
public ResponseEntity<Object> handleControllerException(HttpMediaTypeNotSupportedException ex, WebRequest req)
{
ErrorResponse errorResponse = null;
ex.printStackTrace();
errorResponse = new ErrorResponse();
errorResponse.setCode(HttpCodes.HTTP_CODE_INTERNAL_ERROR);
errorResponse.setError(ex.getMessage());
return new ResponseEntity<Object>(errorResponse,
HttpStatus.valueOf(errorResponse.getCode()));
}
}
現在,我需要進行一些更改,以使其適用於所有異常,包括引發的控制器,但這將在以后提供。 (感謝亞當)。
重要說明:如果同時存在全局處理程序和實體處理程序,spring將忽略全局處理程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.