From fcc03494a46ab67761398f267b22124547a89bdc Mon Sep 17 00:00:00 2001 From: wangyu096 Date: Fri, 17 Nov 2023 16:04:08 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20API=20=E8=AF=B7=E6=B1=82=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E6=94=AF=E6=8C=81=E5=AE=9A=E5=88=B6JSON?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=8F=E5=88=97=E5=8C=96=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=20#2622?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../job/common/util/json/JsonUtilsTest.java | 70 +++++++++++++++++++ .../common/esb/sdk/AbstractBkApiClient.java | 19 +++-- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/backend/commons/common-utils/src/test/java/com/tencent/bk/job/common/util/json/JsonUtilsTest.java b/src/backend/commons/common-utils/src/test/java/com/tencent/bk/job/common/util/json/JsonUtilsTest.java index c526451db9..0d2146be0f 100644 --- a/src/backend/commons/common-utils/src/test/java/com/tencent/bk/job/common/util/json/JsonUtilsTest.java +++ b/src/backend/commons/common-utils/src/test/java/com/tencent/bk/job/common/util/json/JsonUtilsTest.java @@ -1,5 +1,6 @@ package com.tencent.bk.job.common.util.json; +import com.fasterxml.jackson.annotation.JsonInclude; import org.joda.time.DateTime; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -34,4 +35,73 @@ public void setDateTime(DateTime dateTime) { TestObj testObj = new TestObj(); assertThatCode(() -> JsonUtils.toJson(testObj)).doesNotThrowAnyException(); } + + @Test + @DisplayName("测试序列化非空的属性") + void testToNonEmptyJson() { + Content content = new Content(); + content.setId("abc"); + assertThat(JsonUtils.toNonEmptyJson(content)).doesNotContain("name"); + } + + @Test + @DisplayName("测试序列化所有属性,包括 null/empty 的") + void testAllOutputJson() { + Content content = new Content(); + content.setId("abc"); + assertThat(JsonUtils.toJson(content)).contains("id", "name"); + } + + @Test + @DisplayName("测试 JsonInclude 注解覆盖 JsonUtils 中的默认的Include 配置") + void testJsonIncludeAnnotation() { + Content2 content = new Content2(); + content.setId("abc"); + assertThat(JsonUtils.toJson(content)).doesNotContain("name"); + } + + + private static class Content { + private String id; + private String name; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + + @JsonInclude(JsonInclude.Include.NON_EMPTY) + private static class Content2 { + private String id; + private String name; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } } diff --git a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/AbstractBkApiClient.java b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/AbstractBkApiClient.java index 871990a870..54bd50f177 100644 --- a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/AbstractBkApiClient.java +++ b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/AbstractBkApiClient.java @@ -36,6 +36,7 @@ import com.tencent.bk.job.common.util.JobContextUtil; import com.tencent.bk.job.common.util.http.ExtHttpHelper; import com.tencent.bk.job.common.util.http.HttpHelperFactory; +import com.tencent.bk.job.common.util.json.JsonMapper; import com.tencent.bk.job.common.util.json.JsonUtils; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Tag; @@ -70,6 +71,7 @@ public abstract class AbstractBkApiClient { * API调用度量指标名称 */ private final String metricName; + private JsonMapper jsonMapper = JsonMapper.nonEmptyMapper(); /** * @param meterRegistry MeterRegistry @@ -94,6 +96,15 @@ public AbstractBkApiClient(MeterRegistry meterRegistry, this.lang = lang; } + /** + * 配置自定义的 JsonMapper, 用于序列化 Json 数据 + * + * @param jsonMapper jsonMapper + */ + public void setJsonMapper(JsonMapper jsonMapper) { + this.jsonMapper = jsonMapper; + } + public EsbResp doRequest(ApiRequestInfo requestInfo, TypeReference> typeReference) { return doRequest(requestInfo, typeReference, defaultHttpHelper, null); @@ -166,7 +177,7 @@ private EsbResp requestApiAndWrapResponse(ApiRequestInfo requestInf throw new InternalException(errorMsg, ErrorCode.API_ERROR); } - esbResp = JsonUtils.fromJson(respStr, typeReference); + esbResp = jsonMapper.fromJson(respStr, typeReference); apiContext.setResp(esbResp); if (!esbResp.isSuccess()) { log.warn( @@ -259,7 +270,7 @@ private String postForString(String url, } String responseBody; Header[] header = buildBkApiRequestHeaders(authorization); - responseBody = httpHelper.post(url, JsonUtils.toJson(body), header); + responseBody = httpHelper.post(url, jsonMapper.toJson(body), header); return responseBody; } @@ -272,7 +283,7 @@ private String putForString(String url, } String responseBody; Header[] header = buildBkApiRequestHeaders(authorization); - responseBody = httpHelper.put(url, "UTF-8", JsonUtils.toJson(body), Arrays.asList(header)); + responseBody = httpHelper.put(url, "UTF-8", jsonMapper.toJson(body), Arrays.asList(header)); return responseBody; } @@ -319,7 +330,7 @@ private Header[] buildBkApiRequestHeaders(BkApiAuthorization authorization) { } private Header buildBkApiAuthorizationHeader(BkApiAuthorization authorization) { - return new BasicHeader(BK_API_AUTH_HEADER, JsonUtils.toJson(authorization)); + return new BasicHeader(BK_API_AUTH_HEADER, jsonMapper.toJson(authorization)); } private String getLangFromRequest() {