Skip to content

Commit 39d6f22

Browse files
committed
Avoid duplicate registration of [RequestBody|ResponseBody]Advice
Prior to this commit, if a @ControllerAdvice bean implemented both RequestBodyAdvice and ResponseBodyAdvice, it was registered twice in RequestMappingHandlerAdapter, leading to duplicate application of the same logic. This commit ensures that such instances are only registered once. Fixes gh-22638
1 parent e6fd7ab commit 39d6f22

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -107,6 +107,7 @@
107107
*
108108
* @author Rossen Stoyanchev
109109
* @author Juergen Hoeller
110+
* @author Sam Brannen
110111
* @since 3.1
111112
* @see HandlerMethodArgumentResolver
112113
* @see HandlerMethodReturnValueHandler
@@ -612,16 +613,18 @@ private void initControllerAdviceCache() {
612613
logger.info("Detected @InitBinder methods in " + adviceBean);
613614
}
614615
}
615-
if (RequestBodyAdvice.class.isAssignableFrom(beanType)) {
616-
requestResponseBodyAdviceBeans.add(adviceBean);
617-
if (logger.isInfoEnabled()) {
618-
logger.info("Detected RequestBodyAdvice bean in " + adviceBean);
619-
}
620-
}
621-
if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
616+
617+
boolean isRequestBodyAdvice = RequestBodyAdvice.class.isAssignableFrom(beanType);
618+
boolean isResponseBodyAdvice = ResponseBodyAdvice.class.isAssignableFrom(beanType);
619+
if (isRequestBodyAdvice || isResponseBodyAdvice) {
622620
requestResponseBodyAdviceBeans.add(adviceBean);
623621
if (logger.isInfoEnabled()) {
624-
logger.info("Detected ResponseBodyAdvice bean in " + adviceBean);
622+
if (isRequestBodyAdvice) {
623+
logger.info("Detected RequestBodyAdvice bean in " + adviceBean);
624+
}
625+
else {
626+
logger.info("Detected ResponseBodyAdvice bean in " + adviceBean);
627+
}
625628
}
626629
}
627630
}

0 commit comments

Comments
 (0)