统一异常处理
This commit is contained in:
53
Chapter3-1-6/src/main/java/com/didispace/dto/ErrorInfo.java
Normal file
53
Chapter3-1-6/src/main/java/com/didispace/dto/ErrorInfo.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package com.didispace.dto;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
package com.didispace.exception;
|
||||
|
||||
import com.didispace.dto.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
|
||||
class GlobalExceptionHandler {
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
|
||||
@@ -18,4 +20,16 @@ class GlobalExceptionHandler {
|
||||
return mav;
|
||||
}
|
||||
|
||||
}
|
||||
@ExceptionHandler(value = MyException.class)
|
||||
@ResponseBody
|
||||
public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, MyException 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace.exception;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/5/2 上午10:50.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
public class MyException extends Exception {
|
||||
|
||||
public MyException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.didispace.web;
|
||||
|
||||
import com.didispace.exception.MyException;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -20,6 +21,11 @@ public class HelloController {
|
||||
throw new Exception("发生错误");
|
||||
}
|
||||
|
||||
@RequestMapping("/json")
|
||||
public String json() throws MyException {
|
||||
throw new MyException("发生错误2");
|
||||
}
|
||||
|
||||
@RequestMapping("/")
|
||||
public String index(ModelMap map) {
|
||||
map.addAttribute("host", "http://blog.didispace.com");
|
||||
|
||||
Reference in New Issue
Block a user