Skip to content

Commit 3444ebb

Browse files
committedSep 11, 2015
Merge branch '1.2.x'
2 parents 93700d0 + 11d59df commit 3444ebb

File tree

6 files changed

+118
-7
lines changed

6 files changed

+118
-7
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.tomcat.jsp;
18+
19+
public class MyException extends RuntimeException {
20+
21+
public MyException(String message) {
22+
super(message);
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.tomcat.jsp;
18+
19+
public class MyRestResponse {
20+
21+
private String message;
22+
23+
public MyRestResponse(String message) {
24+
this.message = message;
25+
}
26+
27+
public String getMessage() {
28+
return this.message;
29+
}
30+
31+
}

‎spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/tomcat/jsp/WelcomeController.java

+20
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import java.util.Map;
2121

2222
import org.springframework.beans.factory.annotation.Value;
23+
import org.springframework.http.HttpStatus;
2324
import org.springframework.stereotype.Controller;
25+
import org.springframework.web.bind.annotation.ExceptionHandler;
2426
import org.springframework.web.bind.annotation.RequestMapping;
27+
import org.springframework.web.bind.annotation.ResponseBody;
28+
import org.springframework.web.bind.annotation.ResponseStatus;
2529

2630
@Controller
2731
public class WelcomeController {
@@ -36,4 +40,20 @@ public String welcome(Map<String, Object> model) {
3640
return "welcome";
3741
}
3842

43+
@RequestMapping("/fail")
44+
public String fail() {
45+
throw new MyException("Oh dear!");
46+
}
47+
48+
@RequestMapping("/fail2")
49+
public String fail2() {
50+
throw new IllegalStateException();
51+
}
52+
53+
@ExceptionHandler(MyException.class)
54+
@ResponseStatus(HttpStatus.BAD_REQUEST)
55+
public @ResponseBody MyRestResponse handleMyRuntimeException(MyException exception) {
56+
return new MyRestResponse("Some data I want to send back to the client.");
57+
}
58+
3959
}

‎spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
117117
ErrorWrapperResponse wrapped = new ErrorWrapperResponse(response);
118118
try {
119119
chain.doFilter(request, wrapped);
120-
int status = wrapped.getStatus();
121-
if (status >= 400) {
122-
handleErrorStatus(request, response, status, wrapped.getMessage());
120+
if (wrapped.hasErrorToSend()) {
121+
handleErrorStatus(request, response, wrapped.getStatus(),
122+
wrapped.getMessage());
123123
response.flushBuffer();
124124
}
125125
else if (!request.isAsyncStarted() && !response.isCommitted()) {
@@ -143,7 +143,6 @@ private void handleErrorStatus(HttpServletRequest request,
143143
handleCommittedResponse(request, null);
144144
return;
145145
}
146-
147146
String errorPath = getErrorPath(this.statuses, status);
148147
if (errorPath == null) {
149148
response.sendError(status, message);
@@ -324,6 +323,10 @@ public String getMessage() {
324323
return this.message;
325324
}
326325

326+
public boolean hasErrorToSend() {
327+
return this.hasErrorToSend;
328+
}
329+
327330
}
328331

329332
}

‎spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
6464

6565
protected final Log logger = LogFactory.getLog(getClass());
6666

67+
private boolean registerErrorPageFilter = true;
68+
69+
/**
70+
* Set if the {@link ErrorPageFilter} should be registered. Set to {@code false} if
71+
* error page mappings should be handled via the Servlet container and not Spring
72+
* Boot.
73+
* @param registerErrorPageFilter if the {@link ErrorPageFilter} should be registered.
74+
*/
75+
protected final void setRegisterErrorPageFilter(boolean registerErrorPageFilter) {
76+
this.registerErrorPageFilter = registerErrorPageFilter;
77+
}
78+
6779
@Override
6880
public void onStartup(ServletContext servletContext) throws ServletException {
6981
WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
@@ -106,7 +118,9 @@ protected WebApplicationContext createRootApplicationContext(
106118
"No SpringApplication sources have been defined. Either override the "
107119
+ "configure method or add an @Configuration annotation");
108120
// Ensure error pages are registered
109-
application.getSources().add(ErrorPageFilter.class);
121+
if (this.registerErrorPageFilter) {
122+
application.getSources().add(ErrorPageFilter.class);
123+
}
110124
return run(application);
111125
}
112126

‎spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import static org.junit.Assert.assertThat;
3939

4040
/**
41-
* Tests for {@link SpringBootServletInitializerTests}.
41+
* Tests for {@link SpringBootServletInitializer}.
4242
*
4343
* @author Phillip Webb
4444
* @author Andy Wilkinson
@@ -82,8 +82,8 @@ public void applicationBuilderCanBeCustomized() throws Exception {
8282
assertThat(servletInitializer.applicationBuilder.built, equalTo(true));
8383
}
8484

85-
@SuppressWarnings("rawtypes")
8685
@Test
86+
@SuppressWarnings("rawtypes")
8787
public void mainClassHasSensibleDefault() throws Exception {
8888
new WithConfigurationAnnotation()
8989
.createRootApplicationContext(this.servletContext);
@@ -93,6 +93,14 @@ public void mainClassHasSensibleDefault() throws Exception {
9393
is(equalTo((Class) WithConfigurationAnnotation.class)));
9494
}
9595

96+
@Test
97+
public void withErrorPageFilterNotRegistered() throws Exception {
98+
new WithErrorPageFilterNotRegistered()
99+
.createRootApplicationContext(this.servletContext);
100+
assertThat(this.application.getSources(),
101+
equalToSet(WithErrorPageFilterNotRegistered.class));
102+
}
103+
96104
private Matcher<? super Set<Object>> equalToSet(Object... items) {
97105
Set<Object> set = new LinkedHashSet<Object>();
98106
Collections.addAll(set, items);
@@ -138,6 +146,16 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio
138146

139147
}
140148

149+
@Configuration
150+
public class WithErrorPageFilterNotRegistered extends
151+
MockSpringBootServletInitializer {
152+
153+
public WithErrorPageFilterNotRegistered() {
154+
setRegisterErrorPageFilter(false);
155+
}
156+
157+
}
158+
141159
@Configuration
142160
public static class Config {
143161

0 commit comments

Comments
 (0)
Please sign in to comment.