本例以Windows, JDK1.7 为基础,来演示使用Spring MVC 对异常的处理和使用自定义异常。

所使用的相关技术:

  1. Spring 4.3.3.RELEASE
  2. Maven 3
  3. JDK 1.7
  4. Tomcat 7

一,项目结构

本项目采用maven构建,项目结构如下:

二,定义异常

本例才用继承RuntimeException来定义异常:

CustomException.java

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
package com.devnp.exception;

public class CustomException extends RuntimeException{

/**
* @author duliu
*/
private static final long serialVersionUID = 1L;

private int errorCode ;

private String errorMsg ;


public CustomException() {
super();
}

public CustomException(int errorCode, String errorMsg) {
super();
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}

public int getErrorCode() {
return errorCode;
}

public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}

public String getErrorMsg() {
return errorMsg;
}

public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}


}

三,异常的处理

在异常的处理上,使用 @ControllerAdvice 注册异常的Controller, 来处理自定义异常和普通异常。
在对异常的处理上,@ExceptionHandler 注解,可以来接受异常的类型。
CustomExceptionController.java

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
package com.devnp.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import com.devnp.exception.CustomException;

@ControllerAdvice
public class CustomExceptionController {

//@ExceptionHandlersince spring 3.0

@ExceptionHandler(CustomException.class)
public ModelAndView handleCustomException(CustomException ex) {

ModelAndView model = new ModelAndView("index");

model.addObject("errorCode", ex.getErrorCode());
model.addObject("errorMsg", ex.getErrorMsg());

return model;

}

@ExceptionHandler(Exception.class)
public ModelAndView handleAllException(Exception ex) {

ModelAndView model = new ModelAndView("index");

model.addObject("errorMsg", "This is IO Exception.class");


return model;

}
}

四,页面

用来显示异常信息

index.jsp

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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Exception</title>
</head>
<body>

<c:if test="${not empty errorCode}">


<h1>${errorCode} : System Errors</h1>


</c:if>

<c:if test="${empty errorCode}">


<h1>System Errors</h1>


</c:if>

<c:if test="${not empty errorMsg}">


<h2>${errorMsg}</h2>


</c:if>

</body>
</html>

五,控制器

字啊控制器上面,采用不同的参数来模拟异常的处理
HelloController.java

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
package com.devnp.controller;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.devnp.exception.CustomException;

@Controller
public class HelloController {

/**
* 1.当传入参数是error, 抛出自定义异常
* 2.当传入参数是io-error, 抛出IO异常
* @param type
* @return
* @throws Exception
*/
@RequestMapping(value = "/{type:.+}", method = RequestMethod.GET)
public ModelAndView getPages(@PathVariable("type") String type) throws Exception {

if ("error".equals(type)) {
//抛出自定义异常
throw new CustomException(1001, "This is custom message");
} else if ("io-error".equals(type)) {
// 抛出系统异常
throw new IOException();

} else {
return new ModelAndView("index").addObject("errorMsg", type);
}

}
}

六,测试

1.访问:http://localhost:7002/Spring4MVCMavenExceptionHandler/duliu

2.访问:http://localhost:7002/Spring4MVCMavenExceptionHandler/error

3.访问:http://localhost:7002/Spring4MVCMavenExceptionHandler/io-error

代码下载

Spring4MVCMavenExceptionHandler.zip