Polish standard Spring MVC exception handling

Rename ExceptionHandlerSupport to ResponseEntityExceptionHandler and
emphasize the contrast to DefaultHandlerExceptionResovler -- i.e.
one returns a ResponseEntity and relies on message converters while
the other returns a ModelAndView and relies on view resolution.

Issue: SPR-9290
This commit is contained in:
Rossen Stoyanchev
2012-08-27 14:06:06 -04:00
parent 0a6b1167a7
commit da05b094f5
7 changed files with 157 additions and 129 deletions

View File

@@ -3843,15 +3843,23 @@ public class SimpleController {
transparently by setting the status of the response. However, it stops short
of writing any error content to the body of the response while your
application may need to add developer-friendly content to every error
response for example when providing a REST API.</para>
response for example when providing a REST API. You can prepare a
<classname>ModelAndView</classname> and
render error content through view resolution -- i.e. by configuring a
<classname>ContentNeogitatingViewResolver</classname>,
<classname>MappingJacksonJsonView</classname>, and so on. However, you
may prefer to use <interfacename>@ExceptionHandler</interfacename>
methods instead.</para>
<para>To achieve this extend <classname>ExceptionHandlerSupport</classname>,
a convenient base class with an <interfacename>@ExceptionHandler</interfacename>
method that handles standard Spring MVC exceptions just as the
<classname>DefaultHandlerExceptionResolver</classname> does but also
allowing you to prepare error content for the body of the response.
See the Javadoc of <classname>ExceptionHandlerSupport</classname>
for more details.</para>
<para>If you prefer to write error content via
<interfacename>@ExceptionHandler</interfacename> methods you can extend
<classname>ResponseEntityExceptionHandler</classname> instead.
This is a convenient base for <interfacename>@ControllerAdvice</interfacename>
classes providing an <interfacename>@ExceptionHandler</interfacename>
method to handle standard Spring MVC exceptions and return
<classname>ResponseEntity</classname>. That allows you to customize the response
and write error content with message converters. See the Javadoc of
<classname>ResponseEntityExceptionHandler</classname> for more details.</para>
</section>
<section id="mvc-ann-annotated-exceptions">

View File

@@ -66,7 +66,7 @@
</section>
<section id="new-in-3.2-webmvc-controller-advice">
<title><interfacename>@ControllerAdvice</interfacename> annotation</title>
<title>New <interfacename>@ControllerAdvice</interfacename> annotation</title>
<para>Classes annotated with <interfacename>@ControllerAdvice</interfacename>
can contain <interfacename>@ExceptionHandler</interfacename>,
@@ -81,13 +81,18 @@
</section>
<section id="new-in-3.2-webmvc-exception-handler-support">
<title><classname>ExceptionHandlerSupport</classname> class</title>
<title>New <classname>ResponseEntityExceptionHandler</classname> class</title>
<para>A convenient base class with an
<interfacename>@ExceptionHandler</interfacename>
method that handles standard Spring MVC exceptions, just as the
<classname>DefaultHandlerExceptionResolver</classname> does, but also
allowing you to prepare error content for the body of the response.</para>
method that handles standard Spring MVC exceptions and returns a
<classname>ResponseEntity</classname> that allowing customizing and writing
the response with HTTP message converters. This servers as an alternative
to the <classname>DefaultHandlerExceptionResolver</classname>, which does
the same but returns a <classname>ModelAndView</classname> instead.</para>
<para>See the revised <xref linkend="mvc-exceptionhandlers"/>
including information on customizing the default Servlet container error page.</para>
</section>