From 1c5343bf5b6af331ac1a27744640353db67ddbc5 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Tue, 13 Sep 2022 17:02:59 +0800 Subject: [PATCH] fix:fix NullPointerException error when using RestTemplate with no label. (#585) --- CHANGELOG.md | 1 + .../router/PolarisRouterServiceInstanceListSupplier.java | 9 +++++++-- .../resttemplate/PolarisLoadBalancerInterceptor.java | 4 ---- spring-cloud-tencent-dependencies/pom.xml | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 839072e91..9f40c2ba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,3 +36,4 @@ - [refactor:optimize project and code.](https://github.com/Tencent/spring-cloud-tencent/pull/570) - [Optimize:change default dynamic config refresh type to reflect.](https://github.com/Tencent/spring-cloud-tencent/pull/574) - [feat:optimize examples.](https://github.com/Tencent/spring-cloud-tencent/pull/577) +- [fix:fix NullPointerException error when using RestTemplate with no label.](https://github.com/Tencent/spring-cloud-tencent/pull/585) diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java index 0c486fc7a..e3acf9fda 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import com.tencent.cloud.common.constant.RouterConstants; import com.tencent.cloud.common.metadata.MetadataContext; @@ -121,8 +122,12 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI Map labelHeaderValuesMap = new HashMap<>(); try { - String labelHeaderValuesContent = labelHeaderValues.stream().findFirst().get(); - labelHeaderValuesMap.putAll(JacksonUtils.deserialize2Map(URLDecoder.decode(labelHeaderValuesContent, UTF_8))); + Optional labelHeaderValuesOptional = labelHeaderValues.stream().findFirst(); + if (labelHeaderValuesOptional.isPresent()) { + String labelHeaderValuesContent = labelHeaderValuesOptional.get(); + labelHeaderValuesMap.putAll( + JacksonUtils.deserialize2Map(URLDecoder.decode(labelHeaderValuesContent, UTF_8))); + } } catch (UnsupportedEncodingException e) { throw new RuntimeException("unsupported charset exception " + UTF_8); diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerInterceptor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerInterceptor.java index 574973d26..8cd4c9144 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerInterceptor.java @@ -134,10 +134,6 @@ public class PolarisLoadBalancerInterceptor extends LoadBalancerInterceptor { labels.putAll(transitiveLabels); // pass label by header - if (labels.size() == 0) { - request.getHeaders().set(RouterConstants.ROUTER_LABEL_HEADER, null); - return; - } String encodedLabelsContent; try { encodedLabelsContent = URLEncoder.encode(JacksonUtils.serialize2Json(labels), UTF_8); diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 8573a2cf7..5f3ec3ad2 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -73,7 +73,7 @@ 1.7.0-2020.0.5-SNAPSHOT - 1.7.4-SNAPSHOT + 1.7.4 1.2.11 4.5.1 1.12.10