BaseController.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.xintong.visualinspection.controller;
  2. import java.io.FileNotFoundException;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.springframework.web.bind.annotation.ControllerAdvice;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import com.alibaba.fastjson.JSON;
  9. import com.xintong.visualinspection.err.BusinessException;
  10. import com.xintong.visualinspection.err.ErrorCode;
  11. /**
  12. * 文件名:TestController
  13. * 版本信息:日期:2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
  14. */
  15. @ControllerAdvice
  16. public class BaseController {
  17. /**
  18. * 返回前台结果结构体
  19. * @return
  20. * String
  21. * @exception
  22. * @since 1.0.0
  23. */
  24. public String returnResult(int result_code,String result_desc, Object o){
  25. Map<String,Object> result = new HashMap<>();
  26. result.put("result_code", result_code);
  27. result.put("result_desc", result_desc);
  28. result.put("result_data", o);
  29. return JSON.toJSON(result).toString();
  30. }
  31. /**
  32. * 返回前台结果结构体
  33. * @return
  34. * String
  35. * @exception
  36. * @since 1.0.0
  37. */
  38. public String returnSuccessResult(String result_desc){
  39. Map<String,Object> result = new HashMap<>();
  40. result.put("result_code", 0);
  41. result.put("result_desc", result_desc);
  42. return JSON.toJSON(result).toString();
  43. }
  44. /**
  45. * 返回前台结果结构体
  46. * @return
  47. * String
  48. * @exception
  49. * @since 1.0.0
  50. */
  51. public String returnSuccessResult(){
  52. Map<String,Object> result = new HashMap<>();
  53. result.put("result_code", 0);
  54. result.put("result_desc", "success");
  55. return JSON.toJSON(result).toString();
  56. }
  57. /**
  58. * 返回前台结果结构体
  59. * @return
  60. * String
  61. * @exception
  62. * @since 1.0.0
  63. */
  64. public String returnSuccessResult(Object o){
  65. Map<String,Object> result = new HashMap<>();
  66. result.put("result_code", 0);
  67. result.put("result_desc", "success");
  68. result.put("result_data", o);
  69. return JSON.toJSON(result).toString();
  70. }
  71. /**
  72. * 返回前台结果结构体
  73. * @return
  74. * String
  75. * @exception
  76. * @since 1.0.0
  77. */
  78. public String returnSuccessResult(String result_desc, Object o){
  79. Map<String,Object> result = new HashMap<>();
  80. result.put("result_code", 0);
  81. result.put("result_desc", result_desc);
  82. result.put("result_data", o);
  83. return JSON.toJSON(result).toString();
  84. }
  85. /** 基于@ExceptionHandler异常处理 */
  86. @ExceptionHandler
  87. public String exp(HttpServletRequest request, Exception ex) {
  88. // 根据不同错误提示不同的错误
  89. ErrorCode code;
  90. if(ex instanceof NullPointerException) {
  91. code = new ErrorCode(10001);
  92. } else if (ex instanceof NumberFormatException) {
  93. code = new ErrorCode(10002);
  94. } else if (ex instanceof IndexOutOfBoundsException) {
  95. code = new ErrorCode(10003);
  96. } else if (ex instanceof ArithmeticException) {
  97. code = new ErrorCode(10004);
  98. } else if (ex instanceof FileNotFoundException) {
  99. code = new ErrorCode(10005);
  100. } else if (ex instanceof IllegalArgumentException) {
  101. code = new ErrorCode(10006);
  102. } else if (ex instanceof BusinessException) {
  103. code = ((BusinessException) ex).getErrCode();
  104. } else{
  105. code = new ErrorCode(11000);
  106. }
  107. return returnResult(code.getCode(),code.getDesc(),null);
  108. }
  109. }