diff --git a/Chapter3-1-6/src/main/java/com/didispace/dto/ErrorInfo.java b/Chapter3-1-6/src/main/java/com/didispace/dto/ErrorInfo.java new file mode 100644 index 0000000..1c1034b --- /dev/null +++ b/Chapter3-1-6/src/main/java/com/didispace/dto/ErrorInfo.java @@ -0,0 +1,53 @@ +package com.didispace.dto; + +public class ErrorInfo { + + 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; + } + +} \ No newline at end of file diff --git a/Chapter3-1-6/src/main/java/com/didispace/exception/GlobalExceptionHandler.java b/Chapter3-1-6/src/main/java/com/didispace/exception/GlobalExceptionHandler.java index fe97105..45d5f0e 100644 --- a/Chapter3-1-6/src/main/java/com/didispace/exception/GlobalExceptionHandler.java +++ b/Chapter3-1-6/src/main/java/com/didispace/exception/GlobalExceptionHandler.java @@ -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; } -} \ No newline at end of file + @ExceptionHandler(value = MyException.class) + @ResponseBody + public ErrorInfo jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception { + ErrorInfo r = new ErrorInfo<>(); + r.setMessage(e.getMessage()); + r.setCode(ErrorInfo.ERROR); + r.setData("Some Data"); + r.setUrl(req.getRequestURL().toString()); + return r; + } + +} + diff --git a/Chapter3-1-6/src/main/java/com/didispace/exception/MyException.java b/Chapter3-1-6/src/main/java/com/didispace/exception/MyException.java new file mode 100644 index 0000000..e6c179e --- /dev/null +++ b/Chapter3-1-6/src/main/java/com/didispace/exception/MyException.java @@ -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); + } + +} diff --git a/Chapter3-1-6/src/main/java/com/didispace/web/HelloController.java b/Chapter3-1-6/src/main/java/com/didispace/web/HelloController.java index 263ebbc..01e5d50 100755 --- a/Chapter3-1-6/src/main/java/com/didispace/web/HelloController.java +++ b/Chapter3-1-6/src/main/java/com/didispace/web/HelloController.java @@ -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");