Skip to content

Commit 0227297

Browse files
顾鲍尔顾鲍尔
authored andcommitted
# add Exception handler
1 parent 8ffd2d3 commit 0227297

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.boylegu.springboot_vue.dao;
2+
3+
/**
4+
* Created by gubaoer on 17/7/2.
5+
*/
6+
7+
public class ErrorInfo<T> {
8+
9+
public static final Integer OK = 0;
10+
public static final Integer ERROR = 100;
11+
12+
private Integer code;
13+
private String message;
14+
private String url;
15+
private T data;
16+
17+
public String getUrl() {
18+
return url;
19+
}
20+
21+
public void setUrl(String url) {
22+
this.url = url;
23+
}
24+
25+
public static Integer getOK() {
26+
return OK;
27+
}
28+
29+
public static Integer getERROR() {
30+
return ERROR;
31+
}
32+
33+
public Integer getCode() {
34+
return code;
35+
}
36+
37+
public void setCode(Integer code) {
38+
this.code = code;
39+
}
40+
41+
public String getMessage() {
42+
return message;
43+
}
44+
45+
public void setMessage(String message) {
46+
this.message = message;
47+
}
48+
49+
public T getData() {
50+
return data;
51+
}
52+
53+
public void setData(T data) {
54+
this.data = data;
55+
}
56+
57+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.boylegu.springboot_vue.exception;
2+
3+
/**
4+
* Created by gubaoer on 17/7/2.
5+
*/
6+
7+
8+
public class ProjException extends Exception {
9+
public ProjException(String message) {
10+
super(message);
11+
}
12+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.boylegu.springboot_vue.exception;
2+
3+
/**
4+
* Created by gubaoer on 17/7/2.
5+
*/
6+
7+
import com.boylegu.springboot_vue.dao.ErrorInfo;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.ResponseBody;
11+
import org.springframework.web.servlet.ModelAndView;
12+
13+
import javax.servlet.http.HttpServletRequest;
14+
15+
@ControllerAdvice
16+
public class ProjExceptionHandler {
17+
18+
@ExceptionHandler(value = Exception.class)
19+
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
20+
ModelAndView mav = new ModelAndView();
21+
mav.addObject("exception", e);
22+
mav.addObject("url", req.getRequestURL());
23+
mav.setViewName("error");
24+
return mav;
25+
}
26+
27+
@ExceptionHandler(value = ProjException.class)
28+
@ResponseBody
29+
public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, ProjException e) throws Exception {
30+
ErrorInfo<String> r = new ErrorInfo<>();
31+
r.setMessage(e.getMessage());
32+
r.setCode(ErrorInfo.ERROR);
33+
r.setData("Some Data");
34+
r.setUrl(req.getRequestURL().toString());
35+
return r;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)