Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update SPR-9290 project

Use @ResponseBody method to customize the default error page of the
servlet container instead of a JSP.
  • Loading branch information...
commit cfd6ed96e240be1fd328944ec99cc3f3ea277538 1 parent 69d213f
@rstoyanchev rstoyanchev authored
View
4 SPR-9290/pom.xml
@@ -134,13 +134,11 @@
</dependency>
-->
- <!-- Jackson JSON Processor
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
- <version>1.8.1</version>
+ <version>1.9.7</version>
</dependency>
- -->
<!-- Rome Atom+RSS
<dependency>
View
31 ...rk/issues/ExceptionRaisingController.java → ...ringframework/issues/ErrorController.java
@@ -15,26 +15,31 @@
*/
package org.springframework.issues;
-import org.springframework.http.HttpStatus;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.bind.annotation.ResponseStatus;
-@Controller
-public class ExceptionRaisingController {
+/**
+ * This controller provides handling for the default error-page declared in web.xml.
+ */
- @RequestMapping(value="/test", method=RequestMethod.GET)
- public void handle() throws Exception {
- throw new ValidationException();
- }
+@Controller
+public class ErrorController {
- @ExceptionHandler(ValidationException.class)
+ @RequestMapping(value="/error", produces="application/json")
@ResponseBody
- @ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Missing request input")
- public void handleException() {
+ public Map<String, Object> handler(HttpServletRequest request) {
+
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put("status", request.getAttribute("javax.servlet.error.status_code"));
+ map.put("reason", request.getAttribute("javax.servlet.error.message"));
+
+ return map;
}
}
View
43 SPR-9290/src/main/java/org/springframework/issues/ExampleController.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2002-2012 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 org.springframework.issues;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+/**
+ * This controller demonstrates raising a {@link ValidationException}, which in
+ * turn is annotated with {@code @ResponseStatus} that specifies an error code
+ * and an error message for the response.
+ *
+ * <p>Whenever {@link HttpServletResponse#sendError(int, String)} is used (or the
+ * response is set to an error status code), servlet containers may render an HTML
+ * error page. This behavior can be customized and some alternative content can
+ * be returned instead. See web.xml and {@link ErrorController}.
+ *
+ */
+@Controller
+public class ExampleController {
+
+ @RequestMapping(value="/example", method=RequestMethod.GET)
+ public void handle() throws Exception {
+ throw new ValidationException();
+ }
+
+}
View
4 SPR-9290/src/main/java/org/springframework/issues/ValidationException.java
@@ -15,6 +15,10 @@
*/
package org.springframework.issues;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Missing request input")
public class ValidationException extends Exception {
private static final long serialVersionUID = 1L;
View
5 SPR-9290/src/main/webapp/WEB-INF/views/error.jsp
@@ -1,5 +0,0 @@
-<%@ page contentType="application/json" pageEncoding="UTF-8"%>
-{
- status:<%=request.getAttribute("javax.servlet.error.status_code") %>,
- reason:<%=request.getAttribute("javax.servlet.error.message") %>
-}
View
2  SPR-9290/src/main/webapp/WEB-INF/views/home.jsp
@@ -7,6 +7,6 @@
</head>
<body>
<h1>Home</h1>
- <p><a href="test">Test</a>
+ <p><a href="example">Test raising exception</a>
</body>
</html>
View
2  SPR-9290/src/main/webapp/WEB-INF/web.xml
@@ -28,7 +28,7 @@
</servlet-mapping>
<error-page>
- <location>/WEB-INF/views/error.jsp</location>
+ <location>/error</location>
</error-page>
<!-- Disables Servlet Container welcome file handling. Needed for compatibility

0 comments on commit cfd6ed9

Please sign in to comment.
Something went wrong with that request. Please try again.