使用@ExceptionHandler示例的Spring MVC异常处理
让我们借助示例来理解这一点:
马文依赖
2) 我们的pom.xml将如下所示
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<项目 XML文件ns="http://maven.apache.org/POM/4.0.0" XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" si:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <模型Version>4.0.0</模型Version> <groupId>com.Arpit.爪哇2blog</groupId> <artifactId>弹簧RestfulWebServicesWithJSONExample</artifactId> <打包>战争</打包> <版>0.0.1-快照</版> <名称>弹簧RestfulWebServicesWithJSONExample 马文 网络应用</名称> <网址>http://maven.apache.org</url> <依存关系> <依赖> <groupId>unit</groupId> <artifactId>unit</artifactId> <版>3.8.1</版> <范围>测试</范围> </依赖> <依赖> <groupId>爪哇x.小服务程序</groupId> <artifactId>爪哇x.小服务程序-api</artifactId> <版>3.1.0</版> </依赖> <依赖> <groupId>组织.弹簧框架</groupId> <artifactId>弹簧-核心</artifactId> <版>${弹簧.版}</版> </依赖> <依赖> <groupId>组织.弹簧框架</groupId> <artifactId>弹簧-网路mvc</artifactId> <版>${弹簧.版}</版> </依赖> <依赖> <groupId>com.fastxml.杰克逊.核心</groupId> <artifactId>杰克逊-数据绑定</artifactId> <版>2.4.1</版> </依赖> </依存关系> <建立> <最后Name>弹簧RestfulWebServicesWithJSONExample</最后Name> <外挂程式> <插入> <groupId>组织.阿帕奇.专家.外挂程式</groupId> <artifactId>专家-编译器-插入</artifactId> <版>3.1</版> <组态> <资源>${杰克.版}</资源> <目标>${杰克.版}</目标> </组态> </插入> </外挂程式> </建立> <属性> <弹簧.版>4.2.1。发布</弹簧.版> <杰克.版>1.7</杰克.版> </属性> </项目> |
弹簧应用程序配置:
3) 如下更改web.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?XML文件 版="1.0" 编码方式=“ UTF-8”?> <网路-应用程式 XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" XML文件ns="http://java.sun.com/xml/ns/javaee" si:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 版="3.0"> <显示-名称>原型 已建立 网页 应用</显示-名称> <欢迎-文件-清单> <欢迎-文件>指数.jsp</欢迎-文件> </欢迎-文件-清单> <小服务程序> <小服务程序-名称>弹簧座</小服务程序-名称> <小服务程序-类> 组织.弹簧框架.网路.小服务程序.分派器 </小服务程序-类> <加载-上-启动>1</加载-上-启动> </小服务程序> <小服务程序-映射> <小服务程序-名称>弹簧座</小服务程序-名称> <网址-模式>/</网址-模式> </小服务程序-映射> </网路-应用程式> |
4) 在/ 网页-INF /文件夹中创建一个名为springrest-servlet.xml的xml文件。
如果您想使用不同的弹簧包来搜索控制器,请更改context:component-scan。请参考 春季MVC 您好 世界示例 了解更多。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<豆子 XML文件ns="http://www.springframework.org/schema/beans" XML文件ns:语境="http://www.springframework.org/schema/context" XML文件ns:多媒体="http://www.springframework.org/schema/mvc" XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" si:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <多媒体:注解-驱动的/> <语境:零件-扫描 基础-包=“ 组织.arpit.java2blog.controller” /> <豆 类=“ 组织.springframework.web.servlet.view.InternalResourceViewResolver”> <属性 名称=“字首”> <值>/网页-INF/</值> </属性> <属性 名称=“后缀”> <值>.jsp</值> </属性> </豆> <多媒体:默认-小服务程序-处理程序/> </豆子> |
创建控制器
6) 创建一个名为的控制器 “HelloWorldController.java” in 包 组织.arpit.java2blog.controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
包 组织.Arpit.爪哇2blog.控制者; 进口 爪哇.io.IO Exception; 进口 组织.阿帕奇.log4j.记录仪; 进口 组织.Arpit.爪哇2blog.例外.自订Exception; 进口 组织.弹簧框架.刻板印象.控制者; 进口 组织.弹簧框架.网路.捆绑.注解.异常处理程序; 进口 组织.弹簧框架.网路.捆绑.注解.路径变量; 进口 组织.弹簧框架.网路.捆绑.注解.请求映射; 进口 组织.弹簧框架.网路.小服务程序.模型和视图; @控制者 上市 类 你好世界Controller { 私人的 静态的 最后 记录仪 记录器 = 记录仪 .getLogger(你好世界Controller.类); @请求映射(“ / 您好world / {helloType}”) 上市 模型和视图 您好(@路径变量(“ 您好Type”) 串 您好Type) 抛出 自订Exception, IO Exception { 串 您好WorldMessage=”; 如果 (您好Type.equalsIgnoreCase(“ 自订Exception”)) { 扔 新 自订Exception(“发生自定义异常”); } 其他 如果 (您好Type.equalsIgnoreCase(“ IO Exception”)) { 扔 新 IO Exception(“发生IO异常”); } 其他 { 您好WorldMessage = “来自java2blog的世界!”; } 返回 新 模型和视图(“您好”, “信息”, 您好WorldMessage); } @异常处理程序(自订Exception.类) 上市 模型和视图 catchCustomException(自订Exception 前) { 模型和视图 模型 = 新 模型和视图(“ 自订_Exception”); 模型.addObject(“错误信息”, 前.getMessage()); 返回 模型; } @异常处理程序(IO Exception.类) 上市 模型和视图 catchIOException(IO Exception 前) { 模型和视图 模型 = 新 模型和视图(“ IO Exception”); 模型.addObject(“错误信息”, 前.getMessage()); 返回 模型; } } |
建立检视
如您所见,我们使用@ExceptionHandler注释了两个方法,即catchCustomException和catchIOException。这些方法将分别处理CustomException和IOException。
修改 index.jsp as以下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@ 页 语言=“ 爪哇” 内容类型=“文本/ html; charset = UTF-8” 页Encoding=“ UTF-8”%> <!DOCTYPE html 上市 “-// W3C // DTD HTML 4.01 Transitional // EN” "http://www.w3.org/TR/html4/loose.dtd"> <html> <头> <元 http-当量=“内容类型” 内容=“文本/ html; charset = UTF-8”> <标题>你好世界</标题> </头> <身体> 您好 世界!! </身体> </html> |
在/ 网页-INF /文件夹中创建Custom_Excepion.jsp来处理CustomException异常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@ 页 语言=“ 爪哇” 内容类型=“文本/ html; charset = ISO-8859-1” 页Encoding=“ ISO-8859-1”%> <!DOCTYPE html 上市 “-// W3C // DTD HTML 4.01 Transitional // EN” "http://www.w3.org/TR/html4/loose.dtd"> <html> <头> <元 http-当量=“内容类型” 内容=“文本/ html; charset = ISO-8859-1”> <标题>自订 例外</标题> </头> <身体> ${错误信息} </身体> </html> |
如下所示在/ 网页-INF文件夹中创建IOException.jsp来处理IOException。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@ 页 语言=“ 爪哇” 内容类型=“文本/ html; charset = ISO-8859-1” 页Encoding=“ ISO-8859-1”%> <!DOCTYPE html 上市 “-// W3C // DTD HTML 4.01 Transitional // EN” "http://www.w3.org/TR/html4/loose.dtd"> <html> <头> <元 http-当量=“内容类型” 内容=“文本/ html; charset = ISO-8859-1”> <标题>IO 例外</标题> </头> <身体> ${错误信息} </身体> </html> |
7) It ‘是时候进行Maven构建了。
运行应用程序
10)让我们点击以下URL
http:// localhost:8080 / 弹簧MVCExceptionalHandlerExample / 您好world / 您好
如您所见,上面的URL可以正常工作。
现在让我们点击一些将引发异常的URL,该异常将由@ExcpetionHandler处理。
当您点击以下URL时。
http:// localhost:8080 / 弹簧MVCExceptionalHandlerExample / 您好world / 自订Exception
如您所见,当您在URL上方单击时,catchException会引发并处理CustomException。
当您点击以下URL时。
http:// localhost:8080 / 弹簧MVCExceptionalHandlerExample / 您好world / IO Exception