Skip to content

Commit b2612e5

Browse files
committed
统一异常处理
1 parent 8beb95f commit b2612e5

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.didispace.dto;
2+
3+
public class ErrorInfo<T> {
4+
5+
public static final Integer OK = 0;
6+
public static final Integer ERROR = 100;
7+
8+
private Integer code;
9+
private String message;
10+
private String url;
11+
private T data;
12+
13+
public String getUrl() {
14+
return url;
15+
}
16+
17+
public void setUrl(String url) {
18+
this.url = url;
19+
}
20+
21+
public static Integer getOK() {
22+
return OK;
23+
}
24+
25+
public static Integer getERROR() {
26+
return ERROR;
27+
}
28+
29+
public Integer getCode() {
30+
return code;
31+
}
32+
33+
public void setCode(Integer code) {
34+
this.code = code;
35+
}
36+
37+
public String getMessage() {
38+
return message;
39+
}
40+
41+
public void setMessage(String message) {
42+
this.message = message;
43+
}
44+
45+
public T getData() {
46+
return data;
47+
}
48+
49+
public void setData(T data) {
50+
this.data = data;
51+
}
52+
53+
}

Chapter3-1-6/src/main/java/com/didispace/exception/GlobalExceptionHandler.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.didispace.exception;
22

3+
import com.didispace.dto.ErrorInfo;
34
import org.springframework.web.bind.annotation.ControllerAdvice;
45
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.ResponseBody;
57
import org.springframework.web.servlet.ModelAndView;
68

79
import javax.servlet.http.HttpServletRequest;
810

911
@ControllerAdvice
10-
class GlobalExceptionHandler {
12+
public class GlobalExceptionHandler {
1113

1214
@ExceptionHandler(value = Exception.class)
1315
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
@@ -18,4 +20,16 @@ public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) thr
1820
return mav;
1921
}
2022

21-
}
23+
@ExceptionHandler(value = MyException.class)
24+
@ResponseBody
25+
public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
26+
ErrorInfo<String> r = new ErrorInfo<>();
27+
r.setMessage(e.getMessage());
28+
r.setCode(ErrorInfo.ERROR);
29+
r.setData("Some Data");
30+
r.setUrl(req.getRequestURL().toString());
31+
return r;
32+
}
33+
34+
}
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace.exception;
2+
3+
/**
4+
* @author 程序猿DD
5+
* @version 1.0.0
6+
* @date 16/5/2 上午10:50.
7+
* @blog http://blog.didispace.com
8+
*/
9+
public class MyException extends Exception {
10+
11+
public MyException(String message) {
12+
super(message);
13+
}
14+
15+
}

Chapter3-1-6/src/main/java/com/didispace/web/HelloController.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.didispace.web;
22

3+
import com.didispace.exception.MyException;
34
import org.springframework.stereotype.Controller;
45
import org.springframework.ui.ModelMap;
56
import org.springframework.web.bind.annotation.RequestMapping;
@@ -20,6 +21,11 @@ public String hello() throws Exception {
2021
throw new Exception("发生错误");
2122
}
2223

24+
@RequestMapping("/json")
25+
public String json() throws MyException {
26+
throw new MyException("发生错误2");
27+
}
28+
2329
@RequestMapping("/")
2430
public String index(ModelMap map) {
2531
map.addAttribute("host", "http://blog.didispace.com");

0 commit comments

Comments
 (0)