From ac5b9c7bd690b4a850a4c1697887ab570850ed82 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 14 Jan 2016 20:25:30 +0100 Subject: [PATCH] Fix javadoc issue in ResponseEntityExceptionHandler Issue: SPR-13869 (cherry picked from commit 73df50d) --- .../ResponseEntityExceptionHandler.java | 81 ++++++++++--------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java index 8e00031dc2..35bb4d4072 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.web.servlet.mvc.method.annotation; import java.util.List; @@ -20,6 +21,7 @@ import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.beans.ConversionNotSupportedException; import org.springframework.beans.TypeMismatchException; import org.springframework.http.HttpHeaders; @@ -43,34 +45,35 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; import org.springframework.web.multipart.support.MissingServletRequestPartException; import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException; +import org.springframework.web.util.WebUtils; /** * A convenient base class for {@link ControllerAdvice @ControllerAdvice} classes * that wish to provide centralized exception handling across all * {@code @RequestMapping} methods through {@code @ExceptionHandler} methods. * - *

This base class provides an {@code @ExceptionHandler} for handling standard - * Spring MVC exceptions that returns a {@code ResponseEntity} to be written with - * {@link HttpMessageConverter message converters}. This is in contrast to + *

This base class provides an {@code @ExceptionHandler} method for handling + * internal Spring MVC exceptions. This method returns a {@code ResponseEntity} + * for writing to the response with a {@link HttpMessageConverter message converter}. + * in contrast to * {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - * DefaultHandlerExceptionResolver} which returns a {@code ModelAndView} instead. + * DefaultHandlerExceptionResolver} which returns a + * {@link org.springframework.web.servlet.ModelAndView ModelAndView}. * - *

If there is no need to write error content to the response body, or if using - * view resolution, e.g. {@code ContentNegotiatingViewResolver}, then use - * {@code DefaultHandlerExceptionResolver} instead. + *

If there is no need to write error content to the response body, or when + * using view resolution (e.g., via {@code ContentNegotiatingViewResolver}), + * then {@code DefaultHandlerExceptionResolver} is good enough. * *

Note that in order for an {@code @ControllerAdvice} sub-class to be * detected, {@link ExceptionHandlerExceptionResolver} must be configured. * * @author Rossen Stoyanchev * @since 3.2 - * + * @see #handleException(Exception, WebRequest) * @see org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver */ public abstract class ResponseEntityExceptionHandler { - protected final Log logger = LogFactory.getLog(getClass()); - /** * Log category to use when no mapped handler is found for a request. * @see #pageNotFoundLogger @@ -78,18 +81,23 @@ public abstract class ResponseEntityExceptionHandler { public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound"; /** - * Additional logger to use when no mapped handler is found for a request. + * Specific logger to use when no mapped handler is found for a request. * @see #PAGE_NOT_FOUND_LOG_CATEGORY */ protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY); + /** + * Common logger for use in subclasses. + */ + protected final Log logger = LogFactory.getLog(getClass()); + /** * Provides handling for standard Spring MVC exceptions. * @param ex the target exception * @param request the current request */ - @ExceptionHandler(value={ + @ExceptionHandler({ NoSuchRequestHandlingMethodException.class, HttpRequestMethodNotSupportedException.class, HttpMediaTypeNotSupportedException.class, @@ -105,9 +113,7 @@ public abstract class ResponseEntityExceptionHandler { BindException.class }) public final ResponseEntity handleException(Exception ex, WebRequest request) { - HttpHeaders headers = new HttpHeaders(); - if (ex instanceof NoSuchRequestHandlingMethodException) { HttpStatus status = HttpStatus.NOT_FOUND; return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, headers, status, request); @@ -169,27 +175,27 @@ public abstract class ResponseEntityExceptionHandler { /** * A single place to customize the response body of all Exception types. - * This method returns {@code null} by default. + *

The default implementation sets the {@link WebUtils#ERROR_EXCEPTION_ATTRIBUTE} + * request attribute and creates a {@link ResponseEntity} from the given + * body, headers, and status. * @param ex the exception - * @param body the body to use for the response - * @param headers the headers to be written to the response - * @param status the selected response status + * @param body the body for the response + * @param headers the headers for the response + * @param status the response status * @param request the current request */ protected ResponseEntity handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) { if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) { - request.setAttribute("javax.servlet.error.exception", ex, WebRequest.SCOPE_REQUEST); + request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST); } - return new ResponseEntity(body, headers, status); } /** * Customize the response for NoSuchRequestHandlingMethodException. - * This method logs a warning and delegates to - * {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method logs a warning and delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -206,8 +212,8 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for HttpRequestMethodNotSupportedException. - * This method logs a warning, sets the "Allow" header, and delegates to - * {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method logs a warning, sets the "Allow" header, and delegates to + * {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -223,14 +229,13 @@ public abstract class ResponseEntityExceptionHandler { if (!supportedMethods.isEmpty()) { headers.setAllow(supportedMethods); } - return handleExceptionInternal(ex, null, headers, status, request); } /** * Customize the response for HttpMediaTypeNotSupportedException. - * This method sets the "Accept" header and delegates to - * {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method sets the "Accept" header and delegates to + * {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -250,7 +255,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for HttpMediaTypeNotAcceptableException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -265,7 +270,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for MissingServletRequestParameterException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -280,7 +285,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for ServletRequestBindingException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -295,7 +300,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for ConversionNotSupportedException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -310,7 +315,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for TypeMismatchException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -325,7 +330,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for HttpMessageNotReadableException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -340,7 +345,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for HttpMessageNotWritableException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -355,7 +360,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for MethodArgumentNotValidException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -370,7 +375,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for MissingServletRequestPartException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status @@ -385,7 +390,7 @@ public abstract class ResponseEntityExceptionHandler { /** * Customize the response for BindException. - * This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. + *

This method delegates to {@link #handleExceptionInternal}. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status