Skip to content

Commit ac5b9c7

Browse files
rstoyanchevjhoeller
authored andcommitted
Fix javadoc issue in ResponseEntityExceptionHandler
Issue: SPR-13869 (cherry picked from commit 73df50d)
1 parent 7c998a7 commit ac5b9c7

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,13 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.servlet.mvc.method.annotation;
1718

1819
import java.util.List;
1920
import java.util.Set;
2021

2122
import org.apache.commons.logging.Log;
2223
import org.apache.commons.logging.LogFactory;
24+
2325
import org.springframework.beans.ConversionNotSupportedException;
2426
import org.springframework.beans.TypeMismatchException;
2527
import org.springframework.http.HttpHeaders;
@@ -43,53 +45,59 @@
4345
import org.springframework.web.context.request.WebRequest;
4446
import org.springframework.web.multipart.support.MissingServletRequestPartException;
4547
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
48+
import org.springframework.web.util.WebUtils;
4649

4750
/**
4851
* A convenient base class for {@link ControllerAdvice @ControllerAdvice} classes
4952
* that wish to provide centralized exception handling across all
5053
* {@code @RequestMapping} methods through {@code @ExceptionHandler} methods.
5154
*
52-
* <p>This base class provides an {@code @ExceptionHandler} for handling standard
53-
* Spring MVC exceptions that returns a {@code ResponseEntity} to be written with
54-
* {@link HttpMessageConverter message converters}. This is in contrast to
55+
* <p>This base class provides an {@code @ExceptionHandler} method for handling
56+
* internal Spring MVC exceptions. This method returns a {@code ResponseEntity}
57+
* for writing to the response with a {@link HttpMessageConverter message converter}.
58+
* in contrast to
5559
* {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
56-
* DefaultHandlerExceptionResolver} which returns a {@code ModelAndView} instead.
60+
* DefaultHandlerExceptionResolver} which returns a
61+
* {@link org.springframework.web.servlet.ModelAndView ModelAndView}.
5762
*
58-
* <p>If there is no need to write error content to the response body, or if using
59-
* view resolution, e.g. {@code ContentNegotiatingViewResolver}, then use
60-
* {@code DefaultHandlerExceptionResolver} instead.
63+
* <p>If there is no need to write error content to the response body, or when
64+
* using view resolution (e.g., via {@code ContentNegotiatingViewResolver}),
65+
* then {@code DefaultHandlerExceptionResolver} is good enough.
6166
*
6267
* <p>Note that in order for an {@code @ControllerAdvice} sub-class to be
6368
* detected, {@link ExceptionHandlerExceptionResolver} must be configured.
6469
*
6570
* @author Rossen Stoyanchev
6671
* @since 3.2
67-
*
72+
* @see #handleException(Exception, WebRequest)
6873
* @see org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
6974
*/
7075
public abstract class ResponseEntityExceptionHandler {
7176

72-
protected final Log logger = LogFactory.getLog(getClass());
73-
7477
/**
7578
* Log category to use when no mapped handler is found for a request.
7679
* @see #pageNotFoundLogger
7780
*/
7881
public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound";
7982

8083
/**
81-
* Additional logger to use when no mapped handler is found for a request.
84+
* Specific logger to use when no mapped handler is found for a request.
8285
* @see #PAGE_NOT_FOUND_LOG_CATEGORY
8386
*/
8487
protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY);
8588

89+
/**
90+
* Common logger for use in subclasses.
91+
*/
92+
protected final Log logger = LogFactory.getLog(getClass());
93+
8694

8795
/**
8896
* Provides handling for standard Spring MVC exceptions.
8997
* @param ex the target exception
9098
* @param request the current request
9199
*/
92-
@ExceptionHandler(value={
100+
@ExceptionHandler({
93101
NoSuchRequestHandlingMethodException.class,
94102
HttpRequestMethodNotSupportedException.class,
95103
HttpMediaTypeNotSupportedException.class,
@@ -105,9 +113,7 @@ public abstract class ResponseEntityExceptionHandler {
105113
BindException.class
106114
})
107115
public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) {
108-
109116
HttpHeaders headers = new HttpHeaders();
110-
111117
if (ex instanceof NoSuchRequestHandlingMethodException) {
112118
HttpStatus status = HttpStatus.NOT_FOUND;
113119
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, headers, status, request);
@@ -169,27 +175,27 @@ else if (ex instanceof BindException) {
169175

170176
/**
171177
* A single place to customize the response body of all Exception types.
172-
* This method returns {@code null} by default.
178+
* <p>The default implementation sets the {@link WebUtils#ERROR_EXCEPTION_ATTRIBUTE}
179+
* request attribute and creates a {@link ResponseEntity} from the given
180+
* body, headers, and status.
173181
* @param ex the exception
174-
* @param body the body to use for the response
175-
* @param headers the headers to be written to the response
176-
* @param status the selected response status
182+
* @param body the body for the response
183+
* @param headers the headers for the response
184+
* @param status the response status
177185
* @param request the current request
178186
*/
179187
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body,
180188
HttpHeaders headers, HttpStatus status, WebRequest request) {
181189

182190
if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) {
183-
request.setAttribute("javax.servlet.error.exception", ex, WebRequest.SCOPE_REQUEST);
191+
request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST);
184192
}
185-
186193
return new ResponseEntity<Object>(body, headers, status);
187194
}
188195

189196
/**
190197
* Customize the response for NoSuchRequestHandlingMethodException.
191-
* This method logs a warning and delegates to
192-
* {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
198+
* <p>This method logs a warning and delegates to {@link #handleExceptionInternal}.
193199
* @param ex the exception
194200
* @param headers the headers to be written to the response
195201
* @param status the selected response status
@@ -206,8 +212,8 @@ protected ResponseEntity<Object> handleNoSuchRequestHandlingMethod(NoSuchRequest
206212

207213
/**
208214
* Customize the response for HttpRequestMethodNotSupportedException.
209-
* This method logs a warning, sets the "Allow" header, and delegates to
210-
* {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
215+
* <p>This method logs a warning, sets the "Allow" header, and delegates to
216+
* {@link #handleExceptionInternal}.
211217
* @param ex the exception
212218
* @param headers the headers to be written to the response
213219
* @param status the selected response status
@@ -223,14 +229,13 @@ protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequest
223229
if (!supportedMethods.isEmpty()) {
224230
headers.setAllow(supportedMethods);
225231
}
226-
227232
return handleExceptionInternal(ex, null, headers, status, request);
228233
}
229234

230235
/**
231236
* Customize the response for HttpMediaTypeNotSupportedException.
232-
* This method sets the "Accept" header and delegates to
233-
* {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
237+
* <p>This method sets the "Accept" header and delegates to
238+
* {@link #handleExceptionInternal}.
234239
* @param ex the exception
235240
* @param headers the headers to be written to the response
236241
* @param status the selected response status
@@ -250,7 +255,7 @@ protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNo
250255

251256
/**
252257
* Customize the response for HttpMediaTypeNotAcceptableException.
253-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
258+
* <p>This method delegates to {@link #handleExceptionInternal}.
254259
* @param ex the exception
255260
* @param headers the headers to be written to the response
256261
* @param status the selected response status
@@ -265,7 +270,7 @@ protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeN
265270

266271
/**
267272
* Customize the response for MissingServletRequestParameterException.
268-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
273+
* <p>This method delegates to {@link #handleExceptionInternal}.
269274
* @param ex the exception
270275
* @param headers the headers to be written to the response
271276
* @param status the selected response status
@@ -280,7 +285,7 @@ protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingSer
280285

281286
/**
282287
* Customize the response for ServletRequestBindingException.
283-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
288+
* <p>This method delegates to {@link #handleExceptionInternal}.
284289
* @param ex the exception
285290
* @param headers the headers to be written to the response
286291
* @param status the selected response status
@@ -295,7 +300,7 @@ protected ResponseEntity<Object> handleServletRequestBindingException(ServletReq
295300

296301
/**
297302
* Customize the response for ConversionNotSupportedException.
298-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
303+
* <p>This method delegates to {@link #handleExceptionInternal}.
299304
* @param ex the exception
300305
* @param headers the headers to be written to the response
301306
* @param status the selected response status
@@ -310,7 +315,7 @@ protected ResponseEntity<Object> handleConversionNotSupported(ConversionNotSuppo
310315

311316
/**
312317
* Customize the response for TypeMismatchException.
313-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
318+
* <p>This method delegates to {@link #handleExceptionInternal}.
314319
* @param ex the exception
315320
* @param headers the headers to be written to the response
316321
* @param status the selected response status
@@ -325,7 +330,7 @@ protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, Ht
325330

326331
/**
327332
* Customize the response for HttpMessageNotReadableException.
328-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
333+
* <p>This method delegates to {@link #handleExceptionInternal}.
329334
* @param ex the exception
330335
* @param headers the headers to be written to the response
331336
* @param status the selected response status
@@ -340,7 +345,7 @@ protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotRead
340345

341346
/**
342347
* Customize the response for HttpMessageNotWritableException.
343-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
348+
* <p>This method delegates to {@link #handleExceptionInternal}.
344349
* @param ex the exception
345350
* @param headers the headers to be written to the response
346351
* @param status the selected response status
@@ -355,7 +360,7 @@ protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWrit
355360

356361
/**
357362
* Customize the response for MethodArgumentNotValidException.
358-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
363+
* <p>This method delegates to {@link #handleExceptionInternal}.
359364
* @param ex the exception
360365
* @param headers the headers to be written to the response
361366
* @param status the selected response status
@@ -370,7 +375,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
370375

371376
/**
372377
* Customize the response for MissingServletRequestPartException.
373-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
378+
* <p>This method delegates to {@link #handleExceptionInternal}.
374379
* @param ex the exception
375380
* @param headers the headers to be written to the response
376381
* @param status the selected response status
@@ -385,7 +390,7 @@ protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletR
385390

386391
/**
387392
* Customize the response for BindException.
388-
* This method delegates to {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}.
393+
* <p>This method delegates to {@link #handleExceptionInternal}.
389394
* @param ex the exception
390395
* @param headers the headers to be written to the response
391396
* @param status the selected response status

0 commit comments

Comments
 (0)