Browse Source

异常处理

Hu Jie 7 years ago
parent
commit
a308253865

+ 7 - 2
src/main/java/com/uas/platform/b2b/listener/GlobalExceptionHandler.java

@@ -13,6 +13,9 @@ import com.uas.platform.core.util.AgentUtils;
 import com.uas.platform.core.util.serializer.FlexJsonUtils;
 import org.apache.http.protocol.HTTP;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -71,7 +74,7 @@ public class GlobalExceptionHandler {
      */
     @ResponseBody
     @ExceptionHandler(value = Exception.class)
-    public HttpServletResponse defaultErrorHandler(HttpServletRequest request, Exception e, HttpServletResponse response) throws UnsupportedEncodingException {
+    public ResponseEntity<String> defaultErrorHandler(HttpServletRequest request, Exception e, HttpServletResponse response) throws UnsupportedEncodingException {
         String jsonStr = JSON.toJSONString(e.getStackTrace());
         List<ExceptionMsg> msgList = FlexJsonUtils.fromJsonArray(jsonStr, ExceptionMsg.class);
         List<ExceptionMsg> msgs = new ArrayList<>();
@@ -91,6 +94,8 @@ public class GlobalExceptionHandler {
         if (!StringUtils.isEmpty(log.getId())) {
             detailDao.save(new ErrorLogDetail(log.getId(), AgentUtils.getIp(request), needRead, StringUtils.isEmpty(data) ? "" : URLDecoder.decode(data, HTTP.UTF_8), access));
         }
-        return response;
+        HttpHeaders headers = new HttpHeaders();
+        headers.add("Content-Type", "application/text; charset=utf-8");
+        return new ResponseEntity(e.getMessage(), headers, HttpStatus.INTERNAL_SERVER_ERROR);
     }
 }