-
Notifications
You must be signed in to change notification settings - Fork 0
/
spring-exception-handling.html
18 lines (17 loc) · 16.3 KB
/
spring-exception-handling.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html><html lang="de-ch"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Spring Exception Handling - Finecloud</title><meta name="description" content="HTTP Status Codes 100 Series - Informational Responses • 200 Series - Successful Responses 300 Series - Redirection Responses 400 Series - Client Error Responses 500 Series - Server Error Responses Use of HTTP Status Codes 200 Series are used when the request completes as…"><meta name="generator" content="Publii Open-Source CMS for Static Site"><link rel="stylesheet" href="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism-black.css"><link rel="canonical" href="https://www.finecloud.ch/spring-exception-handling.html"><link rel="alternate" type="application/atom+xml" href="https://www.finecloud.ch/feed.xml"><link rel="alternate" type="application/json" href="https://www.finecloud.ch/feed.json"><meta property="og:title" content="Spring Exception Handling"><meta property="og:site_name" content="Finecloud"><meta property="og:description" content="HTTP Status Codes 100 Series - Informational Responses • 200 Series - Successful Responses 300 Series - Redirection Responses 400 Series - Client Error Responses 500 Series - Server Error Responses Use of HTTP Status Codes 200 Series are used when the request completes as…"><meta property="og:url" content="https://www.finecloud.ch/spring-exception-handling.html"><meta property="og:type" content="article"><link rel="shortcut icon" href="https://www.finecloud.ch/media/website/finecloud.png" type="image/png"><link rel="stylesheet" href="https://www.finecloud.ch/assets/css/style.css?v=39da73365516a098a9b73b721fc970e2"><script type="application/ld+json">{"@context":"http://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.finecloud.ch/spring-exception-handling.html"},"headline":"Spring Exception Handling","datePublished":"2023-04-10T08:36","dateModified":"2023-04-10T08:45","description":"HTTP Status Codes 100 Series - Informational Responses • 200 Series - Successful Responses 300 Series - Redirection Responses 400 Series - Client Error Responses 500 Series - Server Error Responses Use of HTTP Status Codes 200 Series are used when the request completes as…","author":{"@type":"Person","name":"Finecloud","url":"https://www.finecloud.ch/authors/finecloud/"},"publisher":{"@type":"Organization","name":"Finecloud"}}</script><meta name="google-site-verification" content="seFY9U12uiEq5U3_MyZiX6XWzk0AVFl9zITr2ZKsytY"></head><body><div class="site-container"><header class="top" id="js-header"><a class="logo" href="https://www.finecloud.ch/">Finecloud</a><nav class="navbar js-navbar"><button class="navbar__toggle js-toggle" aria-label="Menu" aria-haspopup="true" aria-expanded="false"><span class="navbar__toggle-box"><span class="navbar__toggle-inner">Menu</span></span></button><ul class="navbar__menu"><li><a href="https://www.finecloud.ch/" target="_self">Blog</a></li><li><a href="https://www.finecloud.ch/tags/" target="_self">Tags</a></li></ul></nav><div class="search"><div class="search__overlay js-search-overlay"><div class="search__overlay-inner"><form action="https://www.finecloud.ch/search.html" class="search__form"><input class="search__input js-search-input" type="search" name="q" placeholder="search..." aria-label="search..." autofocus="autofocus"></form><button class="search__close js-search-close" aria-label="Close">Close</button></div></div><button class="search__btn js-search-btn" aria-label="Search"><svg role="presentation" focusable="false"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#search"/></svg></button></div></header><main><article class="post"><div class="hero"><figure class="hero__image hero__image--overlay"><img src="https://www.finecloud.ch/media/website/download.jpg" srcset="https://www.finecloud.ch/media/website/responsive/download-xs.jpg 300w, https://www.finecloud.ch/media/website/responsive/download-sm.jpg 480w, https://www.finecloud.ch/media/website/responsive/download-md.jpg 768w, https://www.finecloud.ch/media/website/responsive/download-lg.jpg 1024w, https://www.finecloud.ch/media/website/responsive/download-xl.jpg 1360w, https://www.finecloud.ch/media/website/responsive/download-2xl.jpg 1600w" sizes="100vw" loading="eager" alt=""></figure><header class="hero__content"><div class="wrapper"><div class="post__meta"><time datetime="2023-04-10T08:36">April 10, 2023</time></div><h1>Spring Exception Handling</h1></div></header></div><div class="wrapper post__entry"><h3>HTTP Status Codes</h3><div class="page" title="Page 2"><div class="section"><div class="layoutArea"><div class="column"><ul><li>100 Series - Informational Responses • 200 Series - Successful Responses</li><li>300 Series - Redirection Responses</li><li>400 Series - Client Error Responses</li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">500 Series - Server Error Responses</span></li></ul><h4>Use of HTTP Status Codes</h4><div class="page" title="Page 2"><div class="section"><div class="layoutArea"><div class="column"><div class="page" title="Page 3"><div class="section"><div class="layoutArea"><div class="column"><ul><li>200 Series are used when the request completes as expected<ul><li>Common codes used: 200 Ok, 201 Created, 202 Accepted, 204 No Content</li></ul></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">400 Series are used when the request is understood but fails an expected condition</span><ul><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">400 Bad Request - Invalid data received</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">401 Unauthorized - User authentication required</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">403 Forbidden - User authenticated, not authorized</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">404 Not Found - Requested Resource Not Found</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">405 Method Not Allowed - Method Not Supported</span></li></ul></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">500 Series are used when there is a server side error</span><br><ul><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Typically from unexpected runtime errors</span></li><li>Null Pointer Errors, parse errors, database connection errors, etc</li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Code should log relevant information</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Do not return stack trace to client</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Be careful to not “leak” information to internet</span></li></ul></li></ul><h3>Standard Spiring MVC Exceptions</h3><ul><li>Spring MVC does support a number of standard exceptions</li><li>Standard Exceptions are handled by the DefaultHandlerExceptionResolver</li><li>The DefaultHandlerExceptionResolver sets the appropriate HTTP status code</li><li>BUT does not write content to the body of the response</li><li>Spring MVC does have robust support for customizing error responses</li><li>BindException - 400 Bad Request</li><li>ConversionNotSupportedException - 500 Internal Server Error</li><li>HttpMediaTypeNotAcceptableException - 406 Not Acceptable</li><li>HttpMediaTypeNotSupportedException - 415 Unsupported Media Type</li><li>HttpMessageNotReadableException - 400 Bad Request</li><li>HttpMessageNotWritableException - 500 Internal Server Error</li><li>HttpRequestMethodNotSupportedException - 405 Method Not Allowed</li><li>MethodArgumentNotValidException - 400 Bad Request</li><li>MissingServletRequestParameterException - 400 Bad Request</li><li>MissingServletRequestPartException - 400 Bad Request</li><li>NoSuchRequestHandlingMethodException - 404 Not Found</li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">TypeMismatchException - 400 Bad Request</span></li></ul></div><div class="column"><h3>Spring Exception Handling</h3><ul><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">@ExceptionHandler on controller method to handle specific Exception types</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">@ReponseStatus - annotation for custom exception classes to set desired HTTP status</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Implement AbstractHandlerException Resolver - full control over response (including body)</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">@ControllerAdvice - used to implement a global exception handler</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">ResponseStatusException.class - (Spring 5+) Exception which can be thrown which allows setting the HTTP status and message in the constructor</span></li></ul><h3>Spring Boot ErrorController</h3><ul><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Provides Whitelabel Error Page for HTML requests, or JSON response for RESTful requests</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Properties:</span><br><ul><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">server.error.include-binding-errors - default: never</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">server.error.include-exception - default: false</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">server.error.include-message - default: never</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">server.error.include-stacktrace - default: never</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">server.error.path - default: /error</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">server.error.whitelabel.enabled - default: true</span></li></ul></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Spring Boot includes a BasicError Controller</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">This class can be extended for additional error response customization</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Allows for wide support of the needs of various clients and situations</span></li><li><span style="color: var(--text-primary-color); font-family: var(--editor-font-family); font-size: inherit; font-weight: var(--font-weight-normal);">Rarely used, but important to know it is available for use when needed</span></li></ul></div></div></div></div></div></div></div></div></div></div></div></div></div><footer class="wrapper post__footer"><p class="post__last-updated">This article was updated on April 10, 2023</p><ul class="post__tag"><li><a href="https://www.finecloud.ch/tags/java/">java</a></li><li><a href="https://www.finecloud.ch/tags/softwareentwicklung/">software development</a></li><li><a href="https://www.finecloud.ch/tags/spring/">spring</a></li><li><a href="https://www.finecloud.ch/tags/spring-framework/">spring-framework</a></li></ul><div class="post__share"></div></footer></article><nav class="post__nav"><div class="post__nav-inner"><div class="post__nav-prev"><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-prev"/></svg> <a href="https://www.finecloud.ch/mockmvc.html" class="post__nav-link" rel="prev"><span>Previous</span> Testing with MockMVC</a></div><div class="post__nav-next"><a href="https://www.finecloud.ch/data-transfer-objects.html" class="post__nav-link" rel="next"><span>Next</span> Data Transfer Objects </a><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-next"/></svg></div></div></nav><div class="post__related related"><div class="wrapper"><h2 class="h5 related__title">You should also read:</h2><article class="related__item"><div class="feed__meta"><time datetime="2023-05-02T21:45" class="feed__date">Mai 2, 2023</time></div><h3 class="h1"><a href="https://www.finecloud.ch/building-spring-boot-docker-images.html">Building Spring Boot Docker Images</a></h3></article><article class="related__item"><div class="feed__meta"><time datetime="2023-04-04T15:29" class="feed__date">April 4, 2023</time></div><h3 class="h1"><a href="https://www.finecloud.ch/spring-bean-lifecycle.html">Spring Bean Lifecycle</a></h3></article><article class="related__item"><div class="feed__meta"><time datetime="2023-04-04T14:57" class="feed__date">April 4, 2023</time></div><h3 class="h1"><a href="https://www.finecloud.ch/spring-annotations.html">Spring Annotations</a></h3></article></div></div></main><footer class="footer"><div class="footer__copyright"><p>Powered by Publii</p></div><button onclick="backToTopFunction()" id="backToTop" class="footer__bttop" aria-label="Back to top" title="Back to top"><svg><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#toparrow"/></svg></button></footer></div><script>window.publiiThemeMenuConfig = {
mobileMenuMode: 'sidebar',
animationSpeed: 300,
submenuWidth: 'auto',
doubleClickTime: 500,
mobileMenuExpandableSubmenus: true,
relatedContainerForOverlayMenuSelector: '.top',
};</script><script defer="defer" src="https://www.finecloud.ch/assets/js/scripts.min.js?v=6ca8b60e6534a3888de1205e82df8528"></script><script>var images = document.querySelectorAll('img[loading]');
for (var i = 0; i < images.length; i++) {
if (images[i].complete) {
images[i].classList.add('is-loaded');
} else {
images[i].addEventListener('load', function () {
this.classList.add('is-loaded');
}, false);
}
}</script><script defer="defer" src="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism.js"></script></body></html>