You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2021/09/16 06:11:03 UTC
[incubator-shenyu] branch master updated: fix "GlobalErrorHandler"
error object to map bug by JacksonUtils.toMap. (#2088)
This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 7ea1a32 fix "GlobalErrorHandler" error object to map bug by JacksonUtils.toMap. (#2088)
7ea1a32 is described below
commit 7ea1a32bf4c4779252d94125e7b6e7f452f48ae2
Author: Qicz <qi...@gmail.com>
AuthorDate: Thu Sep 16 14:10:58 2021 +0800
fix "GlobalErrorHandler" error object to map bug by JacksonUtils.toMap. (#2088)
* fix "GlobalErrorHandler" error object to map bug by JacksonUtils.toMap.
* fix global test
---
.../org/apache/shenyu/common/utils/JsonUtils.java | 19 +++++++++++++++++++
.../apache/shenyu/web/handler/GlobalErrorHandler.java | 4 ++--
.../shenyu/web/handler/GlobalErrorHandlerTest.java | 2 +-
3 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/JsonUtils.java b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/JsonUtils.java
index 5bd9a87..b958846 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/JsonUtils.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/JsonUtils.java
@@ -22,6 +22,7 @@ import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
@@ -38,6 +39,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
+import java.util.LinkedHashMap;
import java.util.Map;
/**
@@ -87,6 +89,23 @@ public final class JsonUtils {
}
/**
+ * Object to Map.
+ *
+ * @param object the object
+ * @return the converted map
+ */
+ public static Map<String, Object> toMap(final Object object) {
+ try {
+ String json = MAPPER.writeValueAsString(object);
+ final MapType mapType = MAPPER.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, Object.class);
+ return MAPPER.readValue(json, mapType);
+ } catch (IOException e) {
+ LOG.warn("write to map error: " + object, e);
+ return new LinkedHashMap<>();
+ }
+ }
+
+ /**
* Remove class object.
*
* @param object the object
diff --git a/shenyu-web/src/main/java/org/apache/shenyu/web/handler/GlobalErrorHandler.java b/shenyu-web/src/main/java/org/apache/shenyu/web/handler/GlobalErrorHandler.java
index 81c465a..21f6abf 100644
--- a/shenyu-web/src/main/java/org/apache/shenyu/web/handler/GlobalErrorHandler.java
+++ b/shenyu-web/src/main/java/org/apache/shenyu/web/handler/GlobalErrorHandler.java
@@ -17,7 +17,7 @@
package org.apache.shenyu.web.handler;
-import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.common.utils.JsonUtils;
import org.apache.shenyu.common.utils.ThreadShare;
import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
import org.slf4j.Logger;
@@ -92,7 +92,7 @@ public class GlobalErrorHandler extends DefaultErrorWebExceptionHandler {
}
HTTP_STATUS_HOLDER.set(httpStatus.value());
Object error = ShenyuResultWrap.error(httpStatus.value(), httpStatus.getReasonPhrase(), ex.getMessage());
- return GsonUtils.getInstance().toObjectMap(GsonUtils.getInstance().toJson(error));
+ return JsonUtils.toMap(error);
}
private void logError(final ServerRequest request) {
diff --git a/shenyu-web/src/test/java/org/apache/shenyu/web/handler/GlobalErrorHandlerTest.java b/shenyu-web/src/test/java/org/apache/shenyu/web/handler/GlobalErrorHandlerTest.java
index bd06405..5816ef9 100644
--- a/shenyu-web/src/test/java/org/apache/shenyu/web/handler/GlobalErrorHandlerTest.java
+++ b/shenyu-web/src/test/java/org/apache/shenyu/web/handler/GlobalErrorHandlerTest.java
@@ -113,7 +113,7 @@ public class GlobalErrorHandlerTest {
.build();
Map<String, Object> response = globalErrorHandler.getErrorAttributes(serverRequest, false);
assertNotNull(response);
- assertThat(response, hasEntry("code", 500L));
+ assertThat(response, hasEntry("code", 500));
assertThat(response, hasEntry("message", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()));
assertThat(response, hasEntry("data", nullPointerException.getMessage()));
}