Merge branch 'refs/heads/feature/DM/nflg-bom' into feature/DM/nflg-bom-transition
This commit is contained in:
commit
dde0fe373f
|
|
@ -9,12 +9,16 @@ import nflg.product.common.vo.ResultVO;
|
|||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author 曹鹏飞
|
||||
|
|
@ -35,13 +39,16 @@ public class LoggingRequestBodyAdvice implements RequestBodyAdvice {
|
|||
|
||||
@Override
|
||||
public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
|
||||
try {
|
||||
String contentType = httpInputMessage.getHeaders().getFirst("Content-Type");
|
||||
if (StrUtil.containsIgnoreCase(contentType, "multipart")) {
|
||||
return httpInputMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
|
||||
try {
|
||||
log.info("请求参数: {}", JsonUtil.toJson(o));
|
||||
String bodyContent;
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpInputMessage.getBody(), StandardCharsets.UTF_8))) {
|
||||
bodyContent = reader.lines().collect(Collectors.joining());
|
||||
log.info("请求参数: {}", bodyContent);
|
||||
}
|
||||
log.info("请求头: {}", JsonUtil.toJson(httpInputMessage.getHeaders()));
|
||||
String token = httpInputMessage.getHeaders().getFirst("Authorization");
|
||||
if (StrUtil.isNotBlank(token)) {
|
||||
|
|
@ -50,9 +57,29 @@ public class LoggingRequestBodyAdvice implements RequestBodyAdvice {
|
|||
log.info("请求用户: {}", JsonUtil.toJson(result.getData()));
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("读取请求体出错", ex);
|
||||
if (StrUtil.isBlank(bodyContent)) {
|
||||
return httpInputMessage;
|
||||
} else {
|
||||
return new HttpInputMessage() {
|
||||
@Override
|
||||
public InputStream getBody() throws IOException {
|
||||
return new ByteArrayInputStream(bodyContent.getBytes());
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return httpInputMessage.getHeaders();
|
||||
}
|
||||
};
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("读取请求数据出错", ex);
|
||||
return httpInputMessage;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
|
||||
return o;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.nflg.product.bomnew.advice;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.nflg.product.bomnew.util.JsonUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
|
@ -32,7 +33,10 @@ public class LoggingResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
|||
@Override
|
||||
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
|
||||
try {
|
||||
String contentType = serverHttpResponse.getHeaders().getFirst("Content-Type");
|
||||
if (!StrUtil.containsIgnoreCase(contentType, "octet-stream")) {
|
||||
log.info("响应数据: {}", JsonUtil.toJson(o));
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("读取响应数据出错", ex);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.nflg.product.base.core.exception;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import nflg.product.common.constant.STATE;
|
||||
import nflg.product.common.vo.ResultVO;
|
||||
|
|
@ -167,4 +168,12 @@ public class BaseGlobalExceptionHandle {
|
|||
//return ResultVO.error(e.getState(), e.getMessage());
|
||||
return ResultVO.error(e.getState(), e.getMessage(), e.getData());
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = InvalidFormatException.class)
|
||||
@ResponseBody
|
||||
public ResultVO handleInvalidFormatException(ErrorMsgException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
//return ResultVO.error(e.getState(), e.getMessage());
|
||||
return ResultVO.error(STATE.ParamErr, "数据格式错误,请修改后再提交: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue