当返回异常时,是这样子的
1. { 2. "timestamp": "2019-12-11T05:37:10.096+0000", 3. "status": 500, 4. "error": "Internal Server Error", 5. "message": "报错了", 6. "path": "/test/testException" 7. }
但是 可能有时前台需要一个code值来判断抛出的错误 这样就需要我们封装去返回code值和message值
同时还可以拦截异常进行输出,是市面架构的通用做法
代码实现
1.自定义一个CustomExcepiton 需要继承Exception (或其他子类)
1. public class TemplateException extends Exception { 2. 3. /** 4. * 服务器状态码 5. */ 6. private Integer code; 7. 8. /** 9. * 错误信息 10. */ 11. private String message; 12. 13. public Integer getCode() { 14. return code; 15. } 16. 17. public void setCode(Integer code) { 18. this.code = code; 19. } 20. 21. @Override 22. public String getMessage() { 23. return message; 24. } 25. 26. public void setMessage(String message) { 27. this.message = message; 28. } 29. 30. public TemplateException(String message) { 31. this.message = message; 32. } 33. 34. public TemplateException(Integer code, String message) { 35. this.message = message; 36. this.code = code; 37. } 38. }
2.定义一个返回实体类
如果不进行处理 直接输出200和成功码
1. public enum CommonEnum { 2. 3. SC_SUCCESS(200, "成功"), 4. 5. SC_NO_JURISDICTION(401, "没有权限,请联系管理员授权"), 6. 7. SC_INTERNAL_SERVER_ERROR_500(500, "接口异常"); 8. 9. }
1. public class Result<T> implements Serializable { 2. 3. /** 4. * 返回编码 5. */ 6. private Integer code = 200; 7. 8. /** 9. * 返回信息 10. */ 11. private String message = "操作成功"; 12. 13. /** 14. * 返回数据 15. */ 16. private T data; 17. 18. /** 19. * 成功标志 20. */ 21. private boolean success = true; 22. 23. public Result() { 24. } 25. 26. public Result(Integer code, String Message) { 27. this.code = code; 28. this.message = Message; 29. } 30. 31. public int getCode() { 32. return code; 33. } 34. 35. public void setCode(Integer code) { 36. this.code = code; 37. } 38. 39. public String getMessage() { 40. return this.message; 41. } 42. 43. public void setMessage(String message) { 44. this.message = message; 45. } 46. 47. public T getData() { 48. return data; 49. } 50. 51. public void setData(T data) { 52. this.data = data; 53. } 54. 55. public void SetResult(Integer code, String msg, T data) { 56. this.setMessage(msg); 57. this.setData(data); 58. this.setCode(code); 59. } 60. 61. public static Result<Object> error(String msg) { 62. return error(CommonEnum.SC_INTERNAL_SERVER_ERROR_500.getCode(), msg); 63. } 64. 65. public static Result<Object> error(int code, String msg) { 66. Result<Object> r = new Result<Object>(); 67. r.setCode(code); 68. r.setMessage(msg); 69. return r; 70. } 71. }
3.定义一个ExceptionHandle 在异常时会进行逻辑处理
1. @RestControllerAdvice 2. public class DemoExceptionHandler { 3. 4. private final static Logger logger = LoggerFactory.getLogger(DemoExceptionHandler.class); 5. 6. /** 7. * 自定义异常处理 8. * 9. * @param e 10. * @return 11. */ 12. @ExceptionHandler(TemplateException.class) 13. public Result<?> handleTemplateException(TemplateException e) { 14. logger.info(e.getMessage()); 15. e.printStackTrace(); 16. return Result.error(e.getCode(), e.getMessage()); 17. } 18. 19. /** 20. * exception异常处理 21. * 22. * @param e 23. * @return 24. */ 25. @ExceptionHandler(Exception.class) 26. public Result<?> handleException(Exception e) { 27. logger.info(e.getMessage()); 28. e.printStackTrace(); 29. return Result.error("接口异常,错误信息为" + e.getMessage()); 30. } 31. 32. /** 33. * 权限异常 34. * 35. * @param e 36. * @return 37. */ 38. @ExceptionHandler({UnauthorizedException.class, AuthorizationException.class}) 39. public Result<?> handleAuthorizationException(AuthorizationException e) { 40. logger.info(e.getMessage()); 41. e.printStackTrace(); 42. return Result.error(CommonEnum.SC_NO_JURISDICTION.getCode(), CommonEnum.SC_NO_JURISDICTION.getMessage()); 43. } 44. }
这时候在抛出异常时 返回值为
1. { 2. "code": 10066, 3. "status": 0, 4. "msg": "报错了", 5. "data": null 6. }