RestExceptionHandler.java
4.52 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package com.cjs.site.util.net;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import com.cjs.site.util.lang.JsonUtil;
import com.cjs.site.util.web.ActionUtil;
/**
* 自定义ExceptionHandler,专门处理Restful异常.
*
* @author calvin
*/
// 会被Spring-MVC自动扫描,但又不属于Controller的annotation。
//@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
private Logger logger = LogManager.getLogger(RestExceptionHandler.class);
/**
* 处理所有的RuntimeException.
* 原本只是处理RestException,但是这样就需要在所有的Control方法中try-catch。所以这里直接
* 处理所有的RuntimeException,精简代码。
*/
@ExceptionHandler(value = { RuntimeException.class })
public final ResponseEntity<?> handleException(RuntimeException ex, WebRequest request) {
HttpHeaders headers = new HttpHeaders();
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
if (ex instanceof RestException) {
status = ((RestException) ex).status;
logger.error(ex.getMessage());
} else {
//注意:只在开发时打印异常堆栈信息
logger.error(getErrorMessage(ex, request), ex);
}
headers.setContentType(MediaType.parseMediaType(MediaTypes.TEXT_PLAIN_UTF_8));
return handleExceptionInternal(ex, ex.getMessage(), headers, status, request);
}
/**获取发生错误时的请求信息*/
private String getErrorMessage(RuntimeException ex, WebRequest request) {
StringBuilder sb = new StringBuilder("\n请求参数:{");
Map<String, String[]> params = request.getParameterMap();
Iterator<String> headers = request.getHeaderNames();
HttpServletRequest httpServletRequest = ActionUtil.getRequest();
for (String param : params.keySet()) {
sb.append("\n\t").append(param).append(":").append(request.getParameter(param));
}
sb.append("\n}");
sb.append("\nIP:" + ActionUtil.getIP());
sb.append("\nURL:");
if (httpServletRequest != null) {
sb.append(httpServletRequest.getMethod() + " ");
}
sb.append(request.getDescription(true));
sb.append("\n头信息:{");
while (headers.hasNext()) {
String header = headers.next();
sb.append("\n\t").append(header).append(":").append(request.getHeader(header));
}
sb.append("\n}");
return sb.toString();
}
/**
* 处理RestException.(停用)
*/
//@ExceptionHandler(value = { RestException.class })
@Deprecated
public final ResponseEntity<?> handleRestException(RestException ex, WebRequest request) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType(MediaTypes.TEXT_PLAIN_UTF_8));
return handleExceptionInternal(ex, ex.getMessage(), headers, ex.status, request);
}
/**
* 处理JSR311 Validation异常.
*/
@ExceptionHandler(value = { ConstraintViolationException.class })
public final ResponseEntity<?> handleException(ConstraintViolationException ex,
WebRequest request) {
Map<String, String> errors = BeanValidators.extractPropertyAndMessage(ex
.getConstraintViolations());
String body = JsonUtil.toJson(errors);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType(MediaTypes.TEXT_PLAIN_UTF_8));
return handleExceptionInternal(ex, body, headers, HttpStatus.BAD_REQUEST, request);
}
}