Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
顾鲍尔
authored and
顾鲍尔
committed
Jul 2, 2017
1 parent
8ffd2d3
commit 0227297
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/com/boylegu/springboot_vue/dao/ErrorInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.boylegu.springboot_vue.dao; | ||
|
||
/** | ||
* Created by gubaoer on 17/7/2. | ||
*/ | ||
|
||
public class ErrorInfo<T> { | ||
|
||
public static final Integer OK = 0; | ||
public static final Integer ERROR = 100; | ||
|
||
private Integer code; | ||
private String message; | ||
private String url; | ||
private T data; | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
public static Integer getOK() { | ||
return OK; | ||
} | ||
|
||
public static Integer getERROR() { | ||
return ERROR; | ||
} | ||
|
||
public Integer getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(Integer code) { | ||
this.code = code; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public void setData(T data) { | ||
this.data = data; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/boylegu/springboot_vue/exception/ProjException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.boylegu.springboot_vue.exception; | ||
|
||
/** | ||
* Created by gubaoer on 17/7/2. | ||
*/ | ||
|
||
|
||
public class ProjException extends Exception { | ||
public ProjException(String message) { | ||
super(message); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/boylegu/springboot_vue/exception/ProjExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.boylegu.springboot_vue.exception; | ||
|
||
/** | ||
* Created by gubaoer on 17/7/2. | ||
*/ | ||
|
||
import com.boylegu.springboot_vue.dao.ErrorInfo; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
@ControllerAdvice | ||
public class ProjExceptionHandler { | ||
|
||
@ExceptionHandler(value = Exception.class) | ||
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { | ||
ModelAndView mav = new ModelAndView(); | ||
mav.addObject("exception", e); | ||
mav.addObject("url", req.getRequestURL()); | ||
mav.setViewName("error"); | ||
return mav; | ||
} | ||
|
||
@ExceptionHandler(value = ProjException.class) | ||
@ResponseBody | ||
public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, ProjException e) throws Exception { | ||
ErrorInfo<String> r = new ErrorInfo<>(); | ||
r.setMessage(e.getMessage()); | ||
r.setCode(ErrorInfo.ERROR); | ||
r.setData("Some Data"); | ||
r.setUrl(req.getRequestURL().toString()); | ||
return r; | ||
} | ||
|
||
} |