春季MVC例外处理非常重要,以确保您不会向客户端发送服务器例外。今天,我们将研究使用 @ExceptionHandler, @ControllerAdvice和 HandlerExceptionResolver的春季例外处理。任何Web应用(/社区/教程/java-web-application-tutorial-for-beginnersJava Web Application Tutorial for Beginners
)都需要良好的设计来处理例外,因为我们不想在我们的应用程序抛弃任何未处理的例外时提供容器生成的页面。
春季例外行为
拥有一个明确的例外处理方法对于任何Web应用程序框架来说都是一个巨大的优势,如果说 [春季MVC框架]( / 社区 / 教程 / 春季MVC教程 "春季MVC教程")在我们的Web应用程序中提供例外和错误处理方面很好。
- 联合国 ** 基于主计长** - 我们可以在控制器类中定义例外处理器方法. 我们需要的只是用排除汉德勒的注释来说明这些方法。 这种注释以例外类作为论据. 所以,如果我们已经定义了其中之一 对于例外类, 那么所有例外 抛出 我们的请求处理方法 这些例外处理器方法就像其他请求处理器方法一样,我们可以构建出错误响应,用不同的错误页面进行响应. 我们还可以发送JSON错误反应,我们稍后将参照我们的例子。 如果定义了多个例外处理器方法,则使用最接近例外类的处理器方法. 例如,如果我们有IOExcession和Excession定义的两种处理器方法,以及我们的请求处理器方法抛出IOExcession,那么IOExcession的处理器方法就会被执行.
- ** 全球例外处理器** -- -- 例外处理是一个贯穿各领域的问题,应当针对我们申请中的所有点数。 我们已经研究了春季AOP. Spring AOP 示例教程 — Spect, Asspect, Point, Point, June, XML 配置 ` , 因此Spring 提供了 QQ CaptainAdvice 的注释, 我们可以用它来定义我们的全球例外处理器 。 Global Controll Advision中的处理器方法与基于Controller的例外处理器方法相同,在控制器类无法处理例外时使用.
- ** Handler Exception Resolver ** - 对于一般例外,我们大多数时间服务于静态页面. " 春季框架 " 提供了 " 万能排除 " 接口,我们可以实施,以创建全球例外处理器。 这种额外的定义全球例外处理器方法背后的原因是"斯普林"框架还提供了默认的执行类,我们可以在我们的"春季豆"配置文件中定义这些类,以获得"春季框架"例外处理的好处. `SimpleMappingException Resolver'是默认的执行类,它允许我们配置例外 绘制可用于某一特定例外情形的资源图。 我们还可以使用它来创建我们自己的全球处理器,同时应用具体的改变,例如记录例外消息。 (_) (英语)
Let's create a Spring MVC project where we will look into the implementation of Controller based, AOP Based and Exception Resolver Based exception and error handling approaches. We will also write a exception handler method that will return JSON response. If you are new to JSON in Spring, read Spring Restful JSON Tutorial. Our final project will look like below image, we will look at all the components of our application one by one.
春季例外处理胃依赖
除了标准的 Spring MVC 依赖之外,我们还需要 Jackson JSON依赖来支持 JSON。
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>com.journaldev.spring</groupId>
6 <artifactId>SpringExceptionHandling</artifactId>
7 <name>SpringExceptionHandling</name>
8 <packaging>war</packaging>
9 <version>1.0.0-BUILD-SNAPSHOT</version>
10 <properties>
11 <java-version>1.6</java-version>
12 <org.springframework-version>4.0.2.RELEASE</org.springframework-version>
13 <org.aspectj-version>1.7.4</org.aspectj-version>
14 <org.slf4j-version>1.7.5</org.slf4j-version>
15 <jackson.databind-version>2.2.3</jackson.databind-version>
16 </properties>
17 <dependencies>
18 <!-- Jackson -->
19 <dependency>
20 <groupId>com.fasterxml.jackson.core</groupId>
21 <artifactId>jackson-databind</artifactId>
22 <version>${jackson.databind-version}</version>
23 </dependency>
24 <!-- Spring -->
25 <dependency>
26 <groupId>org.springframework</groupId>
27 <artifactId>spring-context</artifactId>
28 <version>${org.springframework-version}</version>
29 <exclusions>
30 <!-- Exclude Commons Logging in favor of SLF4j -->
31 <exclusion>
32 <groupId>commons-logging</groupId>
33 <artifactId>commons-logging</artifactId>
34 </exclusion>
35 </exclusions>
36 </dependency>
37 <dependency>
38 <groupId>org.springframework</groupId>
39 <artifactId>spring-webmvc</artifactId>
40 <version>${org.springframework-version}</version>
41 </dependency>
42
43 <!-- AspectJ -->
44 <dependency>
45 <groupId>org.aspectj</groupId>
46 <artifactId>aspectjrt</artifactId>
47 <version>${org.aspectj-version}</version>
48 </dependency>
49
50 <!-- Logging -->
51 <dependency>
52 <groupId>org.slf4j</groupId>
53 <artifactId>slf4j-api</artifactId>
54 <version>${org.slf4j-version}</version>
55 </dependency>
56 <dependency>
57 <groupId>org.slf4j</groupId>
58 <artifactId>jcl-over-slf4j</artifactId>
59 <version>${org.slf4j-version}</version>
60 <scope>runtime</scope>
61 </dependency>
62 <dependency>
63 <groupId>org.slf4j</groupId>
64 <artifactId>slf4j-log4j12</artifactId>
65 <version>${org.slf4j-version}</version>
66 <scope>runtime</scope>
67 </dependency>
68 <dependency>
69 <groupId>log4j</groupId>
70 <artifactId>log4j</artifactId>
71 <version>1.2.15</version>
72 <exclusions>
73 <exclusion>
74 <groupId>javax.mail</groupId>
75 <artifactId>mail</artifactId>
76 </exclusion>
77 <exclusion>
78 <groupId>javax.jms</groupId>
79 <artifactId>jms</artifactId>
80 </exclusion>
81 <exclusion>
82 <groupId>com.sun.jdmk</groupId>
83 <artifactId>jmxtools</artifactId>
84 </exclusion>
85 <exclusion>
86 <groupId>com.sun.jmx</groupId>
87 <artifactId>jmxri</artifactId>
88 </exclusion>
89 </exclusions>
90 <scope>runtime</scope>
91 </dependency>
92
93 <!-- @Inject -->
94 <dependency>
95 <groupId>javax.inject</groupId>
96 <artifactId>javax.inject</artifactId>
97 <version>1</version>
98 </dependency>
99
100 <!-- Servlet -->
101 <dependency>
102 <groupId>javax.servlet</groupId>
103 <artifactId>servlet-api</artifactId>
104 <version>2.5</version>
105 <scope>provided</scope>
106 </dependency>
107 <dependency>
108 <groupId>javax.servlet.jsp</groupId>
109 <artifactId>jsp-api</artifactId>
110 <version>2.1</version>
111 <scope>provided</scope>
112 </dependency>
113 <dependency>
114 <groupId>javax.servlet</groupId>
115 <artifactId>jstl</artifactId>
116 <version>1.2</version>
117 </dependency>
118
119 <!-- Test -->
120 <dependency>
121 <groupId>junit</groupId>
122 <artifactId>junit</artifactId>
123 <version>4.7</version>
124 <scope>test</scope>
125 </dependency>
126 </dependencies>
127 <build>
128 <plugins>
129 <plugin>
130 <artifactId>maven-eclipse-plugin</artifactId>
131 <version>2.9</version>
132 <configuration>
133 <additionalProjectnatures>
134 <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
135 </additionalProjectnatures>
136 <additionalBuildcommands>
137 <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
138 </additionalBuildcommands>
139 <downloadSources>true</downloadSources>
140 <downloadJavadocs>true</downloadJavadocs>
141 </configuration>
142 </plugin>
143 <plugin>
144 <groupId>org.apache.maven.plugins</groupId>
145 <artifactId>maven-compiler-plugin</artifactId>
146 <version>2.5.1</version>
147 <configuration>
148 <source>1.6</source>
149 <target>1.6</target>
150 <compilerArgument>-Xlint:all</compilerArgument>
151 <showWarnings>true</showWarnings>
152 <showDeprecation>true</showDeprecation>
153 </configuration>
154 </plugin>
155 <plugin>
156 <groupId>org.codehaus.mojo</groupId>
157 <artifactId>exec-maven-plugin</artifactId>
158 <version>1.2.1</version>
159 <configuration>
160 <mainClass>org.test.int1.Main</mainClass>
161 </configuration>
162 </plugin>
163 </plugins>
164 </build>
165</project>
我已经更新了 Spring Framework、AspectJ、Jackson 和 slf4j 版本,以使用最新的版本。
春季MVC例外处理部署描述器
我们的 web.xml 文件看起来像下面。
1<?xml version="1.0" encoding="UTF-8"?>
2<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee"
3 xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
5
6 <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
7 <context-param>
8 <param-name>contextConfigLocation</param-name>
9 <param-value>/WEB-INF/spring/root-context.xml</param-value>
10 </context-param>
11
12 <!-- Creates the Spring Container shared by all Servlets and Filters -->
13 <listener>
14 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15 </listener>
16
17 <!-- Processes application requests -->
18 <servlet>
19 <servlet-name>appServlet</servlet-name>
20 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
21 <init-param>
22 <param-name>contextConfigLocation</param-name>
23 <param-value>/WEB-INF/spring/spring.xml</param-value>
24 </init-param>
25 <load-on-startup>1</load-on-startup>
26 </servlet>
27
28 <servlet-mapping>
29 <servlet-name>appServlet</servlet-name>
30 <url-pattern>/</url-pattern>
31 </servlet-mapping>
32
33 <error-page>
34 <error-code>404</error-code>
35 <location>/resources/404.jsp</location>
36 </error-page>
37</web-app>
大部分是为了插入我们的Web应用程序的春季框架,除了对404错误定义的错误页面。所以当我们的应用程序扔404错误时,这个页面将被用作响应。
春季例外处理 - 模型类
我已将 Employee bean 定义为模型类别,但我们将在我们的应用程序中使用它来返回特定场景中的有效答案。
1package com.journaldev.spring.model;
2
3public class Employee {
4
5 private String name;
6 private int id;
7
8 public String getName() {
9 return name;
10 }
11
12 public void setName(String name) {
13 this.name = name;
14 }
15
16 public int getId() {
17 return id;
18 }
19
20 public void setId(int id) {
21 this.id = id;
22 }
23}
由于我们也将返回 JSON 响应,所以我们可以创建一个具有例外细节的 java 信号,这些细节将作为响应发送。
1package com.journaldev.spring.model;
2
3public class ExceptionJSONInfo {
4
5 private String url;
6 private String message;
7
8 public String getUrl() {
9 return url;
10 }
11 public void setUrl(String url) {
12 this.url = url;
13 }
14 public String getMessage() {
15 return message;
16 }
17 public void setMessage(String message) {
18 this.message = message;
19 }
20}
春季例外处理 - 定制例外类
让我们创建一个自定义的例外类,由我们的应用程序使用。
1package com.journaldev.spring.exceptions;
2
3import org.springframework.http.HttpStatus;
4import org.springframework.web.bind.annotation.ResponseStatus;
5
6@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="Employee Not Found") //404
7public class EmployeeNotFoundException extends Exception {
8
9 private static final long serialVersionUID = -3332292346834265371L;
10
11 public EmployeeNotFoundException(int id){
12 super("EmployeeNotFoundException with id="+id);
13 }
14}
请注意,我们可以使用@ResponseStatus
注释与异常类来定义我们的应用程序将发送的HTTP代码,当我们的应用程序投放这种类型的异常并通过我们的异常处理实现处理时。您可以看到我将HTTP状态设置为404并且我们为此定义了错误页面,所以我们的应用程序应该使用此类型的异常的错误页面,如果我们不返回任何视图。
春季MVC例外处理控制器类例外处理器
让我们看看我们的控制器类别,在那里我们会抛出不同类型的例外。
1package com.journaldev.spring.controllers;
2
3import java.io.IOException;
4import java.sql.SQLException;
5
6import javax.servlet.http.HttpServletRequest;
7
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
10import org.springframework.stereotype.Controller;
11import org.springframework.ui.Model;
12import org.springframework.web.bind.annotation.ExceptionHandler;
13import org.springframework.web.bind.annotation.PathVariable;
14import org.springframework.web.bind.annotation.RequestMapping;
15import org.springframework.web.bind.annotation.RequestMethod;
16import org.springframework.web.bind.annotation.ResponseBody;
17import org.springframework.web.servlet.ModelAndView;
18
19import com.journaldev.spring.exceptions.EmployeeNotFoundException;
20import com.journaldev.spring.model.Employee;
21import com.journaldev.spring.model.ExceptionJSONInfo;
22
23@Controller
24public class EmployeeController {
25
26 private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
27
28 @RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
29 public String getEmployee(@PathVariable("id") int id, Model model) throws Exception{
30 //deliberately throwing different types of exception
31 if(id==1){
32 throw new EmployeeNotFoundException(id);
33 }else if(id==2){
34 throw new SQLException("SQLException, id="+id);
35 }else if(id==3){
36 throw new IOException("IOException, id="+id);
37 }else if(id==10){
38 Employee emp = new Employee();
39 emp.setName("Pankaj");
40 emp.setId(id);
41 model.addAttribute("employee", emp);
42 return "home";
43 }else {
44 throw new Exception("Generic Exception, id="+id);
45 }
46
47 }
48
49 @ExceptionHandler(EmployeeNotFoundException.class)
50 public ModelAndView handleEmployeeNotFoundException(HttpServletRequest request, Exception ex){
51 logger.error("Requested URL="+request.getRequestURL());
52 logger.error("Exception Raised="+ex);
53
54 ModelAndView modelAndView = new ModelAndView();
55 modelAndView.addObject("exception", ex);
56 modelAndView.addObject("url", request.getRequestURL());
57
58 modelAndView.setViewName("error");
59 return modelAndView;
60 }
61}
请注意,对于 EmployeeNotFoundException 处理器,我正在返回 ModelAndView,因此 http 状态代码将被发送为OK(200)。如果它会返回无效,那么 http 状态代码将被发送为 404. 我们将在我们的全球例外处理器实现中研究这种类型的实现。
@ControllerAdvice 和 @ExceptionHandler
这里是我们的全球例外处理器控制器类别。请注意,该类被注明为 @ControllerAdvice 注释。
1package com.journaldev.spring.controllers;
2
3import java.io.IOException;
4import java.sql.SQLException;
5
6import javax.servlet.http.HttpServletRequest;
7
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
10import org.springframework.http.HttpStatus;
11import org.springframework.web.bind.annotation.ControllerAdvice;
12import org.springframework.web.bind.annotation.ExceptionHandler;
13import org.springframework.web.bind.annotation.ResponseStatus;
14
15@ControllerAdvice
16public class GlobalExceptionHandler {
17
18 private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
19
20 @ExceptionHandler(SQLException.class)
21 public String handleSQLException(HttpServletRequest request, Exception ex){
22 logger.info("SQLException Occured:: URL="+request.getRequestURL());
23 return "database_error";
24 }
25
26 @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="IOException occured")
27 @ExceptionHandler(IOException.class)
28 public void handleIOException(){
29 logger.error("IOException handler executed");
30 //returning 404 error code
31 }
32}
请注意,对于 SQLException,我返回 database_error.jsp 作为响应页面,HTTP状态代码为 200。对于 IOException,我们返回valid 以状态代码为 404,所以我们的错误页面将在这种情况下使用。
经销商解决方案
我们只是扩展SimpleMappingExceptionResolver并超级一种方法,但我们可以超级它最重要的方法resolveException
用于日志和发送不同类型的视图页面,但这与使用ControllerAdvice实现相同,所以我要离开它。
春季例外处理配置文件
我们的 spring bean 配置文件看起来如下。 spring.xml 代码:
1<?xml version="1.0" encoding="UTF-8"?>
2<beans:beans xmlns="https://www.springframework.org/schema/mvc"
3 xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
4 xmlns:beans="https://www.springframework.org/schema/beans"
5 xmlns:context="https://www.springframework.org/schema/context"
6 xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
7 https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
8 https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
9
10 <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
11
12 <!-- Enables the Spring MVC @Controller programming model -->
13 <annotation-driven />
14
15 <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
16 <resources mapping="/resources/**" location="/resources/" />
17
18 <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
19 <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20 <beans:property name="prefix" value="/WEB-INF/views/" />
21 <beans:property name="suffix" value=".jsp" />
22 </beans:bean>
23
24
25 <beans:bean id="simpleMappingExceptionResolver" class="com.journaldev.spring.resolver.MySimpleMappingExceptionResolver">
26 <beans:property name="exceptionMappings">
27 <beans:map>
28 <beans:entry key="Exception" value="generic_error"></beans:entry>
29 </beans:map>
30 </beans:property>
31 <beans:property name="defaultErrorView" value="generic_error"/>
32 </beans:bean>
33
34 <!-- Configure to plugin JSON as request and response in method handler -->
35 <beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
36 <beans:property name="messageConverters">
37 <beans:list>
38 <beans:ref bean="jsonMessageConverter"/>
39 </beans:list>
40 </beans:property>
41 </beans:bean>
42
43 <!-- Configure bean to convert JSON to POJO and vice versa -->
44 <beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
45 </beans:bean>
46
47 <context:component-scan base-package="com.journaldev.spring" />
48
49</beans:beans>
请注意,在我们的 Web 应用程序中配置了支持 JSON 的豆类,唯一与例外处理相关的部分是 simpleMappingExceptionResolver 豆类定义,我们将 generic_error.jsp 定义为 Exception 类的视图页面,这确保我们的应用程序未处理的任何例外不会作为响应发送服务器生成的错误页面。
春季 MVC 例外处理 JSP 查看页面
是时候看看我们的应用程序的最后一部分,我们的视图页面,将被用于我们的应用程序。
1<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
2<%@ page session="false" %>
3<html>
4<head>
5 <title>Home</title>
6</head>
7<body>
8 <h3>Hello ${employee.name}!</h3><br>
9 <h4>Your ID is ${employee.id}</h4>
10</body>
11</html>
home.jsp 用于用有效的数据回复,即当我们在客户端请求中获得 id 时为 10。
1<%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7<title>404 Error Page</title>
8</head>
9<body>
10
11<h2>Resource Not Found Error Occured, please contact support.</h2>
12
13</body>
14</html>
404.jsp 用于生成 404 http 状态代码的视图,对于我们的实现,这应该是当我们在客户端请求中获得 id 时的响应。
1<%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
4<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8<title>Error Page</title>
9</head>
10<body>
11<h2>Application Error, please contact support.</h2>
12
13<h3>Debug Information:</h3>
14
15Requested URL= ${url}<br><br>
16
17Exception= ${exception.message}<br><br>
18
19<strong>Exception Stack Trace</strong><br>
20<c:forEach items="${exception.stackTrace}" var="ste">
21 ${ste}
22</c:forEach>
23
24</body>
25</html>
error.jsp 用于当我们的控制器类请求处理方法投放 EmployeeNotFoundException 时,当客户端请求中的 id 值为 1 时,我们应该收到此页面作为回应。
1<%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7<title>Database Error Page</title>
8</head>
9<body>
10
11<h2>Database Error, please contact support.</h2>
12
13</body>
14</html>
database_error.jsp 用于当我们的应用程序投放 SQLException,如在 GlobalExceptionHandler 类中配置. 当客户端请求中的 id 值为 2 时,我们应该收到此页面作为响应。
1<%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7<title>Generic Error Page</title>
8</head>
9<body>
10
11<h2>Unknown Error Occured, please contact support.</h2>
12
13</body>
14</html>
当任何例外发生时,该页面应该是响应,而不是由我们的应用程序代码处理,而 simpleMappingExceptionResolver bean 会照顾到这一点,当客户端请求中的 id 值为 1,2,3 或 10 时,我们应该得到这个页面作为响应。
春季MVC例外处理应用程序
Just deploy the application in the servlet container you are using, I am using Apache Tomcat 7 for this example. Below images show the different response pages returned by our application based on the id value. ID=10, valid response. ID=1, controller based exception handler used
ID=2, global exception handler used with view as response
ID=3, 404 error page used
ID=4, simpleMappingExceptionResolver used for response view
As you can see that we got the expected response in all the cases.
春季例外处理器 JSON 响应
我们几乎完成了我们的教程,除了最后一点,我将解释如何从例外处理方法发送JSON响应,我们的应用程序有所有JSON依赖和 jsonMessageConverter配置,我们需要实现例外处理方法。
1@ExceptionHandler(EmployeeNotFoundException.class)
2 public @ResponseBody ExceptionJSONInfo handleEmployeeNotFoundException(HttpServletRequest request, Exception ex){
3
4 ExceptionJSONInfo response = new ExceptionJSONInfo();
5 response.setUrl(request.getRequestURL().toString());
6 response.setMessage(ex.getMessage());
7
8 return response;
9 }
Now when we use id as 1 in client request, we get following JSON response as shown in the below image. That's all for Spring Exception Handling and Spring MVC Exception Handling, please download the application from below URL and play around with it to learn more.
[下载《春季例外处理项目》(LINK0)]