Commit 656646f7 by qiuweili123

add springboot2.2.5

parent a9b2085d
package com.secoo.mall.common.serializer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.util.IOUtils;
import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.core.serializer.MatrixSerializer;
import lombok.Setter;
......@@ -15,42 +18,29 @@ public class FastJsonSerializer<T> implements MatrixSerializer<T> {
this.type = type;
}
private final static ParserConfig defaultRedisConfig = new ParserConfig();
@Override
public byte[] serialize(T t) {
if (t == null) {
static {
defaultRedisConfig.setAutoTypeSupport(true);
}
public byte[] serialize(T object) {
if (object == null) {
return new byte[0];
}
try {
return JSON.toJSONBytes(
fastJsonConfig.getCharset(),
t,
fastJsonConfig.getSerializeConfig(),
fastJsonConfig.getSerializeFilters(),
fastJsonConfig.getDateFormat(),
JSON.DEFAULT_GENERATE_FEATURE,
fastJsonConfig.getSerializerFeatures()
);
return JSON.toJSONBytes(object, SerializerFeature.WriteClassName);
} catch (Exception ex) {
throw new SystemInternalException();
}
}
@Override
public T deserialize(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
try {
return (T) JSON.parseObject(
bytes,
fastJsonConfig.getCharset(),
type,
fastJsonConfig.getParserConfig(),
fastJsonConfig.getParseProcess(),
JSON.DEFAULT_PARSER_FEATURE,
fastJsonConfig.getFeatures()
);
return JSON.parseObject(new String(bytes, IOUtils.UTF8), type, defaultRedisConfig);
} catch (Exception ex) {
throw new SystemInternalException();
}
......
......@@ -19,6 +19,18 @@
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -42,6 +42,10 @@
<artifactId>spring-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
</dependencies>
<build>
<resources>
......
......@@ -13,8 +13,9 @@ import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
public class ProtocolExceptionHandler {
public abstract class AbsProtocolExceptionHandler {
@Resource
private MessageSource messageSource;
......@@ -25,6 +26,8 @@ public class ProtocolExceptionHandler {
@ExceptionHandler({Exception.class})
public Object exceptionHandler(Exception e) {
try {
logReqParams();
if (exceptionProcessor == null) {
throw e;
}
......@@ -57,8 +60,13 @@ public class ProtocolExceptionHandler {
try {
return messageSource.getMessage(e.getMsg(), e.getArgs(), LocaleContextHolder.getLocale());
} catch (Exception ex) {
return String.format("[%s] not exsits.Please check config message_%s.properties.",e.getMsg(), LocaleContextHolder.getLocale());
return String.format("[%s] not exsits.Please check config message_%s.properties.", e.getMsg(), LocaleContextHolder.getLocale());
}
}
/**
* 打印请求参数包括args和reqbody
*/
protected abstract void logReqParams();
}
......@@ -3,7 +3,7 @@ package com.secoo.mall.dubbo.listener;
import com.secoo.mall.common.core.exception.BusinessException;
import com.secoo.mall.common.core.exception.ParameterException;
import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.handler.ProtocolExceptionHandler;
import com.secoo.mall.common.handler.AbsProtocolExceptionHandler;
import com.secoo.mall.common.util.response.ResponseUtil;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
......@@ -12,7 +12,7 @@ import org.springframework.web.method.annotation.ExceptionHandlerMethodResolver;
import java.lang.reflect.Method;
public class MatrixResponseExceptionListener extends ProtocolExceptionHandler implements ExceptionListener {
public class MatrixResponseExceptionListener extends AbsProtocolExceptionHandler implements ExceptionListener {
private ExceptionHandlerMethodResolver resolver;
public MatrixResponseExceptionListener() {
......@@ -52,4 +52,9 @@ public class MatrixResponseExceptionListener extends ProtocolExceptionHandler im
}
return exception;
}
@Override
protected void logReqParams() {
//unimpimplements
}
}
package com.secoo.mall.web.advice;
import com.secoo.mall.common.handler.ProtocolExceptionHandler;
import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.handler.AbsProtocolExceptionHandler;
import com.secoo.mall.common.util.file.IOUtil;
import com.secoo.mall.common.util.response.ResponseUtil;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.common.util.web.WebUtil;
import com.secoo.mall.web.annotation.ApiController;
import com.secoo.mall.web.annotation.ApiIgnoreJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Optional;
@RestControllerAdvice(annotations = ApiController.class)
public class ControllerResponseAdvice extends ProtocolExceptionHandler implements ResponseBodyAdvice<Object> {
@Slf4j
public class ControllerResponseAdvice extends AbsProtocolExceptionHandler implements ResponseBodyAdvice<Object>, RequestBodyAdvice {
private final static String REQUEST_BODY = "requestBody";
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
......@@ -26,4 +47,66 @@ public class ControllerResponseAdvice extends ProtocolExceptionHandler implement
return ResponseUtil.getSuccessResponse(o);
}
@Override
protected void logReqParams() {
HttpServletRequest request = WebUtil.getRequest();
String uri = request.getRequestURI();
String queryString = request.getQueryString();
String params = getParams(request);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("uri:").append(uri).append(",");
if (StringUtil.isNotEmpty(queryString)) {
stringBuilder.append("queryString:").append(queryString).append(",");
}
if (StringUtil.isNotEmpty(params)) {
stringBuilder.append("params:").append(params);
}
log.info("req:{}", stringBuilder);
}
private String getParams(HttpServletRequest request) {
StringBuilder stringBuilder = new StringBuilder();
// 获取内容格式
String contentType = request.getContentType();
if (StringUtil.isNotBlank(contentType)) {
contentType = contentType.split(";")[0];
}
// form表单格式 表单形式可以从 ParameterMap中获取
if (MediaType.APPLICATION_FORM_URLENCODED_VALUE.equalsIgnoreCase(contentType)) {
// 获取参数
Map<String, String[]> parameterMap = request.getParameterMap();
if (parameterMap != null) {
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
stringBuilder.append(entry.getKey()).append(":").append(entry.getValue()[0]);
}
}
}
Optional.ofNullable(WebUtil.getAttribute(REQUEST_BODY)).ifPresent(param -> stringBuilder.append(param));
return stringBuilder.toString();
}
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return methodParameter.getParameterAnnotation(RequestBody.class) != null;
}
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
return httpInputMessage;
}
@Override
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
WebUtil.setAttribute(REQUEST_BODY, JSON.toJSONString(o));
return o;
}
@Override
public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return o;
}
}
......@@ -49,7 +49,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.9.RELEASE</version>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
......@@ -230,8 +230,19 @@
<artifactId>kryo</artifactId>
<version>4.0.2</version>
</dependency>
<!--protobuf-->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.11.4</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.11.4</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment