-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
36 lines (31 loc) · 1.39 KB
/
GlobalExceptionHandler.java
File metadata and controls
36 lines (31 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.xys.exhandler.controller;
import com.xys.exhandler.exception.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @author xiongyongshun
* @version 1.0
* @created 16/8/29 13:10
*/
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {
private Logger logger = LoggerFactory.getLogger("GlobalExceptionHandler");
@ExceptionHandler(value = BaseException.class)
@ResponseBody
public Object baseErrorHandler(HttpServletRequest req, Exception e) throws Exception {
logger.error("---BaseException Handler---Host {} invokes url {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage());
return "---BaseException Handler---:" + e.getMessage();
}
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Object defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
logger.error("---DefaultException Handler---Host {} invokes url {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage());
return "---DefaultException Handler---:" + e.getMessage();
}
}