修正错误包名
This commit is contained in:
@@ -1,161 +1,161 @@
|
|||||||
package com.gzzn.omms.adminapi.exception.handler;
|
package com.gzzn.omms.adminapi.exception.handler;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.AfterReturning;
|
import org.aspectj.lang.annotation.AfterReturning;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.annotation.Before;
|
||||||
import org.aspectj.lang.annotation.Pointcut;
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
import org.jboss.logging.MDC;
|
import org.jboss.logging.MDC;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
import org.springframework.web.util.ContentCachingRequestWrapper;
|
import org.springframework.web.util.ContentCachingRequestWrapper;
|
||||||
import org.springframework.web.util.ContentCachingResponseWrapper;
|
import org.springframework.web.util.ContentCachingResponseWrapper;
|
||||||
import org.springframework.web.util.WebUtils;
|
import org.springframework.web.util.WebUtils;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.gzzn.omms.adminapi.utils.JsonUtil;
|
import com.gzzn.omms.adminapi.utils.JsonUtil;
|
||||||
|
|
||||||
@Aspect
|
@Aspect
|
||||||
@Component
|
@Component
|
||||||
public class ExceptionAspect {
|
public class ExceptionAspect {
|
||||||
private final static Logger logger = LoggerFactory.getLogger(ExceptionAspect.class);
|
private final static Logger logger = LoggerFactory.getLogger(ExceptionAspect.class);
|
||||||
|
|
||||||
@Pointcut("execution(public * com.gzzn.zdgov.controller.*.*(..))")
|
@Pointcut("execution(public * com.gzzn.omms.adminapi.controller.*.*(..))")
|
||||||
public void log(){
|
public void log(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before("log()")
|
@Before("log()")
|
||||||
public void doBefore(JoinPoint joinPoint){
|
public void doBefore(JoinPoint joinPoint){
|
||||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
HttpServletRequest request = attributes.getRequest();
|
HttpServletRequest request = attributes.getRequest();
|
||||||
|
|
||||||
ContentCachingRequestWrapper wrapperRequest = new ContentCachingRequestWrapper(request);
|
ContentCachingRequestWrapper wrapperRequest = new ContentCachingRequestWrapper(request);
|
||||||
|
|
||||||
MDC.clear();
|
MDC.clear();
|
||||||
MDC.put("request_id", getRequestId(request));
|
MDC.put("request_id", getRequestId(request));
|
||||||
|
|
||||||
//url
|
//url
|
||||||
try {
|
try {
|
||||||
logger.info("request={url={},method={},content_type={},body={},params={},remoteip={}}",
|
logger.info("request={url={},method={},content_type={},body={},params={},remoteip={}}",
|
||||||
request.getRequestURL(),
|
request.getRequestURL(),
|
||||||
request.getMethod(),
|
request.getMethod(),
|
||||||
request.getContentType(),
|
request.getContentType(),
|
||||||
JsonUtil.getString(joinPoint.getArgs()),
|
JsonUtil.getString(joinPoint.getArgs()),
|
||||||
getRequestParams(wrapperRequest),
|
getRequestParams(wrapperRequest),
|
||||||
request.getRemoteAddr()
|
request.getRemoteAddr()
|
||||||
);
|
);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@AfterReturning(pointcut = "log()",returning = "object")//打印输出结果
|
@AfterReturning(pointcut = "log()",returning = "object")//打印输出结果
|
||||||
public void doAfterReturing(Object object){
|
public void doAfterReturing(Object object){
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
String jsonStr;
|
String jsonStr;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
jsonStr = objectMapper.writeValueAsString(object);
|
jsonStr = objectMapper.writeValueAsString(object);
|
||||||
}
|
}
|
||||||
catch (JsonProcessingException e)
|
catch (JsonProcessingException e)
|
||||||
{
|
{
|
||||||
jsonStr = "[unknown]";
|
jsonStr = "[unknown]";
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("response={}",jsonStr);
|
logger.info("response={}",jsonStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param response
|
* @param response
|
||||||
*/
|
*/
|
||||||
private String getResponseBody(ContentCachingResponseWrapper response) {
|
private String getResponseBody(ContentCachingResponseWrapper response) {
|
||||||
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
|
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
|
||||||
if(wrapper != null) {
|
if(wrapper != null) {
|
||||||
byte[] buf = wrapper.getContentAsByteArray();
|
byte[] buf = wrapper.getContentAsByteArray();
|
||||||
if(buf.length > 0) {
|
if(buf.length > 0) {
|
||||||
String payload;
|
String payload;
|
||||||
try {
|
try {
|
||||||
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
|
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
payload = "[unknown]";
|
payload = "[unknown]";
|
||||||
}
|
}
|
||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打印请求参数
|
* 打印请求参数
|
||||||
* @param request
|
* @param request
|
||||||
*/
|
*/
|
||||||
private String getRequestBody(ContentCachingRequestWrapper request) {
|
private String getRequestBody(ContentCachingRequestWrapper request) {
|
||||||
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
|
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
|
||||||
if(wrapper != null) {
|
if(wrapper != null) {
|
||||||
byte[] buf = wrapper.getContentAsByteArray();
|
byte[] buf = wrapper.getContentAsByteArray();
|
||||||
if(buf.length > 0) {
|
if(buf.length > 0) {
|
||||||
String payload;
|
String payload;
|
||||||
try {
|
try {
|
||||||
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
|
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
payload = "[unknown]";
|
payload = "[unknown]";
|
||||||
}
|
}
|
||||||
return payload.replaceAll("\\n","");
|
return payload.replaceAll("\\n","");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取请求地址上的参数
|
* 获取请求地址上的参数
|
||||||
* @param request
|
* @param request
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getRequestParams(HttpServletRequest request) {
|
public static String getRequestParams(HttpServletRequest request) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
Enumeration<String> enu = request.getParameterNames();
|
Enumeration<String> enu = request.getParameterNames();
|
||||||
//获取请求参数
|
//获取请求参数
|
||||||
while (enu.hasMoreElements()) {
|
while (enu.hasMoreElements()) {
|
||||||
String name = enu.nextElement();
|
String name = enu.nextElement();
|
||||||
sb.append(name + "=").append(request.getParameter(name));
|
sb.append(name + "=").append(request.getParameter(name));
|
||||||
if(enu.hasMoreElements()) {
|
if(enu.hasMoreElements()) {
|
||||||
sb.append(", ");
|
sb.append(", ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String getRequestId(HttpServletRequest request)
|
private String getRequestId(HttpServletRequest request)
|
||||||
{
|
{
|
||||||
String requestId = null;
|
String requestId = null;
|
||||||
String parameterRequestId = request.getParameter("requestId");
|
String parameterRequestId = request.getParameter("requestId");
|
||||||
String headerRequestId = request.getHeader("requestId");
|
String headerRequestId = request.getHeader("requestId");
|
||||||
if (parameterRequestId == null && headerRequestId == null)
|
if (parameterRequestId == null && headerRequestId == null)
|
||||||
{
|
{
|
||||||
requestId = UUID.randomUUID().toString();
|
requestId = UUID.randomUUID().toString();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
requestId = parameterRequestId != null ? parameterRequestId : headerRequestId;
|
requestId = parameterRequestId != null ? parameterRequestId : headerRequestId;
|
||||||
}
|
}
|
||||||
|
|
||||||
return requestId;
|
return requestId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user