#init commit
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package {{ .package }}.common.response;public class FailData {
|
||||
private int code = 500;
|
||||
private String message;
|
||||
|
||||
public FailData() {
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +1,19 @@
|
||||
package {{ .package }}.common.response;
|
||||
|
||||
import java.text.*;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class MyDateFormat extends DateFormat {
|
||||
private static final long serialVersionUID = -4580955831439573829L;
|
||||
private static final String customDateFormat = "yyyy-MM-dd HH:mm:ss";
|
||||
private DateFormat dateFormat;
|
||||
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
public MyDateFormat() {
|
||||
this.calendar = Calendar.getInstance();
|
||||
this.dateFormat = simpleDateFormat;
|
||||
}
|
||||
|
||||
|
||||
public MyDateFormat(DateFormat dateFormat) {
|
||||
this.calendar = Calendar.getInstance();
|
||||
this.dateFormat = dateFormat;
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package {{ .package }}.common.response;
|
||||
import com.fasterxml.jackson.core.JsonParser.Feature;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class ObjectMapperFactory {
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public ObjectMapperFactory() {
|
||||
}
|
||||
|
||||
public static ObjectMapper getDefaultObjectMapper() {
|
||||
return objectMapper.copy();
|
||||
}
|
||||
|
||||
static {
|
||||
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).disable(SerializationFeature.FAIL_ON_EMPTY_BEANS).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).configure(Feature.ALLOW_SINGLE_QUOTES, true);
|
||||
SimpleModule bigDecimalModule = new SimpleModule();
|
||||
bigDecimalModule.addSerializer(BigDecimal.class, new ToStringSerializer());
|
||||
objectMapper.registerModule(bigDecimalModule);
|
||||
objectMapper.setDateFormat(new MyDateFormat());
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,11 @@
|
||||
package {{ .package }}.common.response;
|
||||
|
||||
import com.vs.common.util.rpc.pub.FailData;
|
||||
import com.vs.ox.common.exception.ErrorCode;
|
||||
import com.vs.ox.common.exception.IgnoredException;
|
||||
import com.vs.ox.common.utils.ObjectMapperFactory;
|
||||
|
||||
import com.vs.ex.BizException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import jakarta.validation.Path;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.Ordered;
|
||||
@@ -24,65 +17,42 @@ import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class ResponseJsonExceptionResolver extends AbstractHandlerExceptionResolver
|
||||
implements InitializingBean {
|
||||
public class ResponseJsonExceptionResolver extends AbstractHandlerExceptionResolver implements InitializingBean {
|
||||
private HttpMessageConverter messageConverter;
|
||||
|
||||
public ResponseJsonExceptionResolver() {}
|
||||
public ResponseJsonExceptionResolver() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
if (messageConverter == null) {
|
||||
messageConverter =
|
||||
new MappingJackson2HttpMessageConverter(
|
||||
ObjectMapperFactory.getDefaultObjectMapper());
|
||||
}
|
||||
}
|
||||
|
||||
public static String getErrorInfoFromException(Exception e) {
|
||||
try {
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
e.printStackTrace(pw);
|
||||
return "\r\n" + sw.toString() + "\r\n";
|
||||
} catch (Exception e2) {
|
||||
return "bad getErrorInfoFromException";
|
||||
messageConverter = new MappingJackson2HttpMessageConverter(ObjectMapperFactory.getDefaultObjectMapper());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ModelAndView doResolveException(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
Object handler,
|
||||
Exception ex) {
|
||||
/** web请求分页单独处理,应对antd框架 */
|
||||
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
||||
FailData failData = new FailData();
|
||||
failData.setMessage(ex.getMessage());
|
||||
if (ex instanceof IgnoredException) {
|
||||
IgnoredException realEx = (IgnoredException) ex;
|
||||
failData.setCode(realEx.getErrorCode());
|
||||
failData.setData(realEx.getData());
|
||||
if (ex instanceof BizException) {
|
||||
BizException realEx = (BizException) ex;
|
||||
failData.setCode(realEx.getCode());
|
||||
failData.setMessage(realEx.getMessage());
|
||||
log.error("execute {} failed with exception", request.getRequestURL(), ex);
|
||||
} else if (ex instanceof ConstraintViolationException) {
|
||||
failData.setCode(ErrorCode.WRONG_PARAMETER);
|
||||
failData.setCode(400);
|
||||
failData.setMessage(getMessage((ConstraintViolationException) ex));
|
||||
} else if (ex instanceof ErrorCode) {
|
||||
failData.setCode(((ErrorCode) ex).getErrorCode());
|
||||
} else {
|
||||
log.error("execute {} failed with exception", request.getRequestURL(), ex);
|
||||
}
|
||||
try {
|
||||
response.setCharacterEncoding("utf-8");
|
||||
messageConverter.write(
|
||||
failData, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
|
||||
messageConverter.write(failData, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
@@ -99,9 +69,7 @@ public class ResponseJsonExceptionResolver extends AbstractHandlerExceptionResol
|
||||
for (ConstraintViolation<?> constraintViolation : e.getConstraintViolations()) {
|
||||
int i = 0;
|
||||
StringBuilder param = new StringBuilder();
|
||||
for (Iterator<Path.Node> iterator = constraintViolation.getPropertyPath().iterator();
|
||||
iterator.hasNext();
|
||||
i++) {
|
||||
for (Iterator<Path.Node> iterator = constraintViolation.getPropertyPath().iterator(); iterator.hasNext(); i++) {
|
||||
Path.Node node = iterator.next();
|
||||
if (i == 0) {
|
||||
continue;
|
||||
@@ -111,8 +79,9 @@ public class ResponseJsonExceptionResolver extends AbstractHandlerExceptionResol
|
||||
}
|
||||
param.append(node.getName());
|
||||
}
|
||||
msgList.add("参数 [ " + param + " ] " + constraintViolation.getMessage());
|
||||
msgList.add("Param [ " + param + " ] " + constraintViolation.getMessage());
|
||||
}
|
||||
return StringUtils.join(msgList.toArray(), ";");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package {{ .package }}.common.response;
|
||||
|
||||
import com.vs.common.util.rpc.pub.SuccessData;
|
||||
import com.vs.ox.common.utils.ObjectMapperFactory;
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -25,9 +22,7 @@ public class ResponseJsonMethodReturnValueHandler
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
if (messageConverter == null) {
|
||||
messageConverter =
|
||||
new MappingJackson2HttpMessageConverter(
|
||||
ObjectMapperFactory.getDefaultObjectMapper());
|
||||
messageConverter = new MappingJackson2HttpMessageConverter(ObjectMapperFactory.getDefaultObjectMapper());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,26 +32,17 @@ public class ResponseJsonMethodReturnValueHandler
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleReturnValue(
|
||||
Object returnValue,
|
||||
MethodParameter returnType,
|
||||
ModelAndViewContainer mavContainer,
|
||||
NativeWebRequest webRequest)
|
||||
throws Exception {
|
||||
mavContainer.setRequestHandled(true);
|
||||
Object result = returnValue;
|
||||
/** Web分页请求的返回按照antd框架要求的格式,不转为SuccessData */
|
||||
result =
|
||||
result == null
|
||||
? new SuccessData(Collections.emptyMap())
|
||||
: new SuccessData(returnValue);
|
||||
ServletServerHttpResponse response =
|
||||
new ServletServerHttpResponse(
|
||||
webRequest.getNativeResponse(HttpServletResponse.class));
|
||||
messageConverter.write(
|
||||
result,
|
||||
new MediaType(
|
||||
MediaType.APPLICATION_JSON, Collections.singletonMap("charset", "utf-8")),
|
||||
response);
|
||||
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
|
||||
try {
|
||||
mavContainer.setRequestHandled(true);
|
||||
String customMessage = ResponseMessageHolder.getMessage();
|
||||
int code = ResponseMessageHolder.getCode();
|
||||
Object result = new SuccessData(code, returnValue, customMessage);
|
||||
|
||||
ServletServerHttpResponse response = new ServletServerHttpResponse(webRequest.getNativeResponse(HttpServletResponse.class));
|
||||
messageConverter.write(result, new MediaType(MediaType.APPLICATION_JSON, Collections.singletonMap("charset", "utf-8")), response);
|
||||
} finally {
|
||||
ResponseMessageHolder.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package {{ .package }}.common.response;
|
||||
public class ResponseMessageHolder {
|
||||
private static final ThreadLocal<String> MESSAGE_HOLDER = new InheritableThreadLocal<>();
|
||||
|
||||
public static void setMessage(String message) {
|
||||
MESSAGE_HOLDER.set(message);
|
||||
}
|
||||
|
||||
public static String getMessage() {
|
||||
return MESSAGE_HOLDER.get();
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
MESSAGE_HOLDER.remove();
|
||||
}
|
||||
|
||||
private static final ThreadLocal<Integer> CODE_HOLDER = new InheritableThreadLocal<>();
|
||||
|
||||
public static void setCode(Integer code) {
|
||||
CODE_HOLDER.set(code);
|
||||
}
|
||||
|
||||
public static Integer getCode() {
|
||||
Integer code = CODE_HOLDER.get();
|
||||
return code == null ? 200 : code;
|
||||
}
|
||||
|
||||
public static void clearCode() {
|
||||
CODE_HOLDER.remove();
|
||||
}
|
||||
|
||||
public static void setCodeAndMessage(Integer code, String message) {
|
||||
setCode(code);
|
||||
setMessage(message);
|
||||
}
|
||||
|
||||
public static void clearAll() {
|
||||
MESSAGE_HOLDER.remove();
|
||||
CODE_HOLDER.remove();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package {{ .package }}.common.response;
|
||||
public class SuccessData {
|
||||
private int code = 200;
|
||||
private Object data;
|
||||
private String message;
|
||||
|
||||
public SuccessData() {
|
||||
}
|
||||
|
||||
public SuccessData(Object data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public SuccessData(int code, Object data) {
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public SuccessData(int code, Object data, String message) {
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
package {{ .package }}.common.response;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
Reference in New Issue
Block a user