Skip to content

Commit c38e989

Browse files
committed
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
1 parent 3e0849a commit c38e989

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

Diff for: spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.util.Assert;
3838
import org.springframework.util.StringUtils;
3939
import org.springframework.validation.BindingResult;
40-
import org.springframework.web.HttpSessionRequiredException;
4140
import org.springframework.web.bind.WebDataBinder;
4241
import org.springframework.web.bind.annotation.ModelAttribute;
4342
import org.springframework.web.bind.support.WebDataBinderFactory;
@@ -115,7 +114,7 @@ public void initModel(NativeWebRequest request, ModelAndViewContainer container,
115114
if (!container.containsAttribute(name)) {
116115
Object value = this.sessionAttributesHandler.retrieveAttribute(request, name);
117116
if (value == null) {
118-
throw new HttpSessionRequiredException("Expected session attribute '" + name + "'", name);
117+
throw new IllegalStateException("Expected session attribute '" + name + "'");
119118
}
120119
container.addAttribute(name, value);
121120
}

Diff for: spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.ui.Model;
2828
import org.springframework.ui.ModelMap;
2929
import org.springframework.validation.BindingResult;
30-
import org.springframework.web.HttpSessionRequiredException;
3130
import org.springframework.web.bind.WebDataBinder;
3231
import org.springframework.web.bind.annotation.ModelAttribute;
3332
import org.springframework.web.bind.annotation.SessionAttributes;
@@ -43,7 +42,7 @@
4342
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
4443

4544
import static org.assertj.core.api.Assertions.assertThat;
46-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
45+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4746
import static org.mockito.BDDMockito.given;
4847
import static org.mockito.Mockito.mock;
4948

@@ -151,7 +150,7 @@ void sessionAttribute() throws Exception {
151150
void sessionAttributeNotPresent() throws Exception {
152151
ModelFactory modelFactory = new ModelFactory(null, null, this.attributeHandler);
153152
HandlerMethod handlerMethod = createHandlerMethod("handleSessionAttr", String.class);
154-
assertThatExceptionOfType(HttpSessionRequiredException.class).isThrownBy(() ->
153+
assertThatIllegalStateException().isThrownBy(() ->
155154
modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod));
156155

157156
// Now add attribute and try again
@@ -164,7 +163,7 @@ void sessionAttributeNotPresent() throws Exception {
164163
void sessionAttributeByType() throws Exception {
165164
ModelFactory modelFactory = new ModelFactory(null, null, this.attributeHandler);
166165
HandlerMethod handlerMethod = createHandlerMethod("handleTestBean", TestBean.class);
167-
assertThatExceptionOfType(HttpSessionRequiredException.class).isThrownBy(() ->
166+
assertThatIllegalStateException().isThrownBy(() ->
168167
modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod));
169168

170169
// Now add attribute and try again

0 commit comments

Comments
 (0)