From c38e9896c7d51b35d5ff80ab498475b6190c77be Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 17 Jun 2024 14:19:10 +0200 Subject: [PATCH] Remove use of ServletException in ModelFactory This commit changes the use of HttpSessionRequiredException in ModelFactory::initModel to an IllegalStateException, because the former extends ServletException and cannot be used in WebFlux. Closes gh-33043 --- .../web/method/annotation/ModelFactory.java | 3 +-- .../web/method/annotation/ModelFactoryTests.java | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java index 884f7caaf2a..d2dbe96836f 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java @@ -37,7 +37,6 @@ import org.springframework.ui.ModelMap; import org.springframework.util.Assert; import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; -import org.springframework.web.HttpSessionRequiredException; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.support.WebDataBinderFactory; @@ -115,7 +114,7 @@ public final class ModelFactory { if (!container.containsAttribute(name)) { Object value = this.sessionAttributesHandler.retrieveAttribute(request, name); if (value == null) { - throw new HttpSessionRequiredException("Expected session attribute '" + name + "'", name); + throw new IllegalStateException("Expected session attribute '" + name + "'"); } container.addAttribute(name, value); } diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java index e52a01813ed..adce474d13c 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java @@ -27,7 +27,6 @@ import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; -import org.springframework.web.HttpSessionRequiredException; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.SessionAttributes; @@ -43,7 +42,7 @@ import org.springframework.web.method.support.ModelAndViewContainer; import org.springframework.web.testfixture.servlet.MockHttpServletRequest; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -151,7 +150,7 @@ class ModelFactoryTests { void sessionAttributeNotPresent() throws Exception { ModelFactory modelFactory = new ModelFactory(null, null, this.attributeHandler); HandlerMethod handlerMethod = createHandlerMethod("handleSessionAttr", String.class); - assertThatExceptionOfType(HttpSessionRequiredException.class).isThrownBy(() -> + assertThatIllegalStateException().isThrownBy(() -> modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod)); // Now add attribute and try again @@ -164,7 +163,7 @@ class ModelFactoryTests { void sessionAttributeByType() throws Exception { ModelFactory modelFactory = new ModelFactory(null, null, this.attributeHandler); HandlerMethod handlerMethod = createHandlerMethod("handleTestBean", TestBean.class); - assertThatExceptionOfType(HttpSessionRequiredException.class).isThrownBy(() -> + assertThatIllegalStateException().isThrownBy(() -> modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod)); // Now add attribute and try again