增加自定义code和message支持
This commit is contained in:
8
.idea/.gitignore
generated
vendored
8
.idea/.gitignore
generated
vendored
@@ -1,8 +0,0 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
10
.idea/misc.xml
generated
10
.idea/misc.xml
generated
@@ -1,14 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/template/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="23" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
9
.idea/modules.xml
generated
9
.idea/modules.xml
generated
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/common-springboot-template-spring5-springboot2.iml" filepath="$PROJECT_DIR$/.idea/common-springboot-template-spring5-springboot2.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/template/template.iml" filepath="$PROJECT_DIR$/template/template.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,26 @@
|
||||
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,9 +1,6 @@
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -54,21 +51,16 @@ public class ResponseJsonExceptionResolver extends AbstractHandlerExceptionResol
|
||||
|
||||
@Override
|
||||
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
||||
/**
|
||||
* web请求分页单独处理,应对antd框架
|
||||
*/
|
||||
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);
|
||||
}
|
||||
@@ -101,7 +93,7 @@ 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,7 +1,5 @@
|
||||
package {{ .package }}.common.response;
|
||||
|
||||
import com.vs.common.util.rpc.pub.SuccessData;
|
||||
import com.vs.ox.common.utils.ObjectMapperFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -15,9 +13,6 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* 处理 Controller 里的返回值,从 Object转为Json
|
||||
*/
|
||||
public class ResponseJsonMethodReturnValueHandler implements HandlerMethodReturnValueHandler, InitializingBean {
|
||||
private HttpMessageConverter messageConverter;
|
||||
|
||||
@@ -35,14 +30,17 @@ public class ResponseJsonMethodReturnValueHandler implements HandlerMethodReturn
|
||||
|
||||
@Override
|
||||
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
|
||||
try {
|
||||
mavContainer.setRequestHandled(true);
|
||||
Object result = returnValue;
|
||||
/**
|
||||
* Web分页请求的返回按照antd框架要求的格式,不转为SuccessData
|
||||
*/
|
||||
result = result == null ? new SuccessData(Collections.emptyMap()) : new SuccessData(returnValue);
|
||||
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,44 @@
|
||||
package {{ .package }}.common.response;
|
||||
|
||||
import java.lang.ThreadLocal;
|
||||
|
||||
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,49 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import com.vs.ox.common.utils.ObjectMapperFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package {{ .package }}.common.utils;
|
||||
|
||||
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 java.text.SimpleDateFormat;
|
||||
|
||||
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);
|
||||
objectMapper.setDateFormat(new MyDateFormat());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user