1. 程式人生 > >spring boot 異常處理(轉)

spring boot 異常處理(轉)

integer stat 全部 control nts custom ref default turn

spring boot在異常的處理中,默認實現了一個EmbeddedServletContainerCustomizer並定義了一個錯誤頁面到”/error”中,在ErrorMvcAutoConfiguration源碼中可以看到

/**
* {@link EmbeddedServletContainerCustomizer} that configures the container‘s error
* pages.
*/

private static class ErrorPageCustomizer

implements EmbeddedServletContainerCustomizer, Ordered {


private final ServerProperties properties;

protected ErrorPageCustomizer(ServerProperties properties) {
this.properties = properties;
}

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.addErrorPages(new
ErrorPage(this.properties.getServletPrefix()
+ this.properties.getError().getPath()));
}

@Override
public int getOrder() {
return 0;
}

}

還配置了一個默認的白板頁面,在源碼可以看到

@Configuration
@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true
)
@Conditional(ErrorTemplateMissingCondition.class)
protected static class WhitelabelErrorViewConfiguration {

private final SpelView defaultErrorView = new SpelView(
"<html><body><h1>Whitelabel Error Page</h1>"
+ "<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>"
+ "<div id=‘created‘>${timestamp}</div>"
+ "<div>There was an unexpected error (type=${error}, status=${status}).</div>"
+ "<div>${message}</div></body></html>");

@Bean(name = "error")
@ConditionalOnMissingBean(name = "error")
public View defaultErrorView() {
return this.defaultErrorView;
}

// If the user adds @EnableWebMvc then the bean name view resolver from
// WebMvcAutoConfiguration disappears, so add it back in to avoid disappointment.
@Bean
@ConditionalOnMissingBean(BeanNameViewResolver.class)
public BeanNameViewResolver beanNameViewResolver() {
BeanNameViewResolver resolver = new BeanNameViewResolver();
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
return resolver;
}

}

在路徑的處理上,定義了一個BasicErrorController來處理異常,ErrorAttributes來裝載錯誤異常到前端

@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {

private final ErrorProperties errorProperties;

//...

@RequestMapping(produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request,
HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
return new ModelAndView("error", model);
}

@RequestMapping
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}

//...}

默認只要你請求的content-type是”text/html”則返回一個白版頁面給你,如果是其他的content-type,則返回一個json的數據給你。

若你想自定義異常,可以通過以下方式來處理

1.你可以默認在classpath:templates下定義一個error.ftl的freemarker模版來定義異常頁面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1> error page !! </h1>

${timestamp?string(‘yyyy-MM-dd HH:mm:ss‘)}<br/><br/>
${status} <br/><br/>
${error} <br/><br/>
${message} <br/><br/>
${path} <br/><br/>

</body>
</html>

2.可以利用@ExceptionHandler來為某個特定的controller攔截異常,並返回一個json數據,當然你也可以定義全部的controller來攔截異常

@ControllerAdvice(basePackageClasses = FooController.class)
public class FooControllerAdvice extends ResponseEntityExceptionHandler {

@ExceptionHandler(YourException.class)
@ResponseBody
ResponseEntity<?> handleControllerException(HttpServletRequest request, Throwable ex) {
HttpStatus status = getStatus(request);
return new ResponseEntity<>(new CustomErrorType(status.value(), ex.getMessage()), status);
}

private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
return HttpStatus.valueOf(statusCode);
}
}

3.還可以自定義server的錯誤頁面

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer(){
return new MyCustomizer();
}

// ...

private static class MyCustomizer implements EmbeddedServletContainerCustomizer {

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/500"));
}
}

spring boot 異常處理(轉)