From 5f250ebbc16ba32d026060565860cc1c7bf25820 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 11 Sep 2015 12:25:48 -0700 Subject: [PATCH] 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 --- .../src/main/java/sample/jsp/MyException.java | 25 +++++++++++++++ .../main/java/sample/jsp/MyRestResponse.java | 31 +++++++++++++++++++ .../java/sample/jsp/WelcomeController.java | 20 ++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/MyException.java create mode 100644 spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/MyRestResponse.java diff --git a/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/MyException.java b/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/MyException.java new file mode 100644 index 0000000000..2974aa9b41 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/MyException.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.jsp; + +public class MyException extends RuntimeException { + + public MyException(String message) { + super(message); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/MyRestResponse.java b/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/MyRestResponse.java new file mode 100644 index 0000000000..c45fdc77d3 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/MyRestResponse.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.jsp; + +public class MyRestResponse { + + private String message; + + public MyRestResponse(String message) { + this.message = message; + } + + public String getMessage() { + return this.message; + } + +} diff --git a/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/WelcomeController.java b/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/WelcomeController.java index 03d439caa3..0c90e86ad3 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/WelcomeController.java +++ b/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/main/java/sample/jsp/WelcomeController.java @@ -20,8 +20,12 @@ import java.util.Date; import java.util.Map; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; @Controller public class WelcomeController { @@ -36,4 +40,20 @@ public class WelcomeController { return "welcome"; } + @RequestMapping("/fail") + public String fail() { + throw new MyException("Oh dear!"); + } + + @RequestMapping("/fail2") + public String fail2() { + throw new IllegalStateException(); + } + + @ExceptionHandler(MyException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public @ResponseBody MyRestResponse handleMyRuntimeException(MyException exception) { + return new MyRestResponse("Some data I want to send back to the client."); + } + }