Skip to content

Commit 5f250eb

Browse files
committed
Add exception endpoints to tomcat-jsp sample
Update `spring-boot-sample-tomcat-jsp` to include endpoints that trigger exceptions. Primarily to aid testing of the ErrorPageFilter. See gh-2745
1 parent 86d5c19 commit 5f250eb

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 25 additions & 0 deletions
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.jsp;
18+
19+
public class MyException extends RuntimeException {
20+
21+
public MyException(String message) {
22+
super(message);
23+
}
24+
25+
}
Lines changed: 31 additions & 0 deletions
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.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/jsp/WelcomeController.java

Lines changed: 20 additions & 0 deletions
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
}

0 commit comments

Comments
 (0)