Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#2623 from wangyu096/issue_2622
Browse files Browse the repository at this point in the history
perf: API 请求客户端支持定制JSON数据序列化属性 TencentBlueKing#2622
  • Loading branch information
jsonwan authored Nov 17, 2023
2 parents 8f223fe + fcc0349 commit 51c57a7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +71,7 @@ public abstract class AbstractBkApiClient {
* API调用度量指标名称
*/
private final String metricName;
private JsonMapper jsonMapper = JsonMapper.nonEmptyMapper();

/**
* @param meterRegistry MeterRegistry
Expand All @@ -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 <T, R> EsbResp<R> doRequest(ApiRequestInfo<T> requestInfo,
TypeReference<EsbResp<R>> typeReference) {
return doRequest(requestInfo, typeReference, defaultHttpHelper, null);
Expand Down Expand Up @@ -166,7 +177,7 @@ private <T, R> EsbResp<R> requestApiAndWrapResponse(ApiRequestInfo<T> 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(
Expand Down Expand Up @@ -259,7 +270,7 @@ private <T> 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;
}

Expand All @@ -272,7 +283,7 @@ private <T> 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;
}

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 51c57a7

Please sign in to comment.