BaseController.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.apache.ibatis.exceptions.TooManyResultsException;
  7. import org.springframework.http.converter.HttpMessageNotReadableException;
  8. import org.springframework.validation.BindingResult;
  9. import org.springframework.validation.ObjectError;
  10. import org.springframework.web.bind.MethodArgumentNotValidException;
  11. import org.springframework.web.bind.annotation.ControllerAdvice;
  12. import org.springframework.web.bind.annotation.ExceptionHandler;
  13. import com.alibaba.fastjson.JSON;
  14. import com.xintong.system.err.BusinessException;
  15. import com.xintong.system.err.ErrorCode;
  16. /**
  17. * 文件名:TestController
  18. * 版本信息:日期:2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
  19. */
  20. @ControllerAdvice
  21. public class BaseController {
  22. /**
  23. * 返回前台结果结构体
  24. * @return
  25. * String
  26. * @exception
  27. * @since 1.0.0
  28. */
  29. public String returnResult(int result_code,String result_desc, Object o){
  30. Map<String,Object> result = new HashMap<>();
  31. result.put("result_code", result_code);
  32. result.put("result_desc", result_desc);
  33. result.put("result_data", o);
  34. return JSON.toJSON(result).toString();
  35. }
  36. /**
  37. * 返回前台结果结构体
  38. * @return
  39. * String
  40. * @exception
  41. * @since 1.0.0
  42. */
  43. public String returnSuccessResult(String result_desc){
  44. Map<String,Object> result = new HashMap<>();
  45. result.put("result_code", 0);
  46. result.put("result_desc", result_desc);
  47. return JSON.toJSON(result).toString();
  48. }
  49. /**
  50. * 返回前台结果结构体
  51. * @return
  52. * String
  53. * @exception
  54. * @since 1.0.0
  55. */
  56. public String returnSuccessResult(){
  57. Map<String,Object> result = new HashMap<>();
  58. result.put("result_code", 0);
  59. result.put("result_desc", "success");
  60. return JSON.toJSON(result).toString();
  61. }
  62. /**
  63. * 返回前台结果结构体
  64. * @return
  65. * String
  66. * @exception
  67. * @since 1.0.0
  68. */
  69. public String returnSuccessResult(Object o){
  70. Map<String,Object> result = new HashMap<>();
  71. result.put("result_code", 0);
  72. result.put("result_desc", "success");
  73. result.put("result_data", o);
  74. return JSON.toJSON(result).toString();
  75. }
  76. /**
  77. * 返回前台结果结构体
  78. * @return
  79. * String
  80. * @exception
  81. * @since 1.0.0
  82. */
  83. public String returnSuccessResult(String result_desc, Object o){
  84. Map<String,Object> result = new HashMap<>();
  85. result.put("result_code", 0);
  86. result.put("result_desc", result_desc);
  87. result.put("result_data", o);
  88. return JSON.toJSON(result).toString();
  89. }
  90. /** 基于@ExceptionHandler异常处理 */
  91. @ExceptionHandler
  92. public String exp(HttpServletRequest request, Exception ex) {
  93. // 根据不同错误提示不同的错误
  94. ErrorCode code;
  95. if(ex instanceof NullPointerException) {
  96. code = new ErrorCode(10001);
  97. } else if (ex instanceof NumberFormatException) {
  98. code = new ErrorCode(10002);
  99. } else if (ex instanceof IndexOutOfBoundsException) {
  100. code = new ErrorCode(10003);
  101. } else if (ex instanceof ArithmeticException) {
  102. code = new ErrorCode(10004);
  103. } else if (ex instanceof FileNotFoundException) {
  104. code = new ErrorCode(10005);
  105. } else if (ex instanceof IllegalArgumentException) {
  106. code = new ErrorCode(10006);
  107. } else if (ex instanceof HttpMessageNotReadableException) {
  108. code = new ErrorCode(10007);
  109. } else if (ex instanceof BusinessException) {
  110. code = ((BusinessException) ex).getErrCode();
  111. } else if(ex.getCause() instanceof TooManyResultsException){
  112. code = new ErrorCode(10008);
  113. } else if (ex instanceof MethodArgumentNotValidException) {
  114. BindingResult bindingResult = ((MethodArgumentNotValidException) ex).getBindingResult();
  115. code = new ErrorCode(20001,getValidatorErrors(bindingResult));
  116. } else{
  117. code = new ErrorCode(11000);
  118. }
  119. return returnResult(code.getCode(),code.getDesc(),null);
  120. }
  121. public static String getValidatorErrors(BindingResult bindingResult){
  122. StringBuffer sb = new StringBuffer();
  123. for(ObjectError err:bindingResult.getAllErrors()){
  124. sb.append("["+err.getDefaultMessage()+"]");
  125. }
  126. return sb.toString();
  127. }
  128. }