format code

pull/413/head
weihu 3 years ago
parent 8b3fe05df3
commit 2830a9eeea

@ -40,4 +40,3 @@
- [Optimize starters auto-configuration. (main)](https://github.com/Tencent/spring-cloud-tencent/pull/391/files) - [Optimize starters auto-configuration. (main)](https://github.com/Tencent/spring-cloud-tencent/pull/391/files)
- [Feature: format code](https://github.com/Tencent/spring-cloud-tencent/pull/394) - [Feature: format code](https://github.com/Tencent/spring-cloud-tencent/pull/394)
- [test: add PostInitPolarisSDKContextTest](https://github.com/Tencent/spring-cloud-tencent/pull/397) - [test: add PostInitPolarisSDKContextTest](https://github.com/Tencent/spring-cloud-tencent/pull/397)
- [bugfix: Improve the endpoint function of spring-cloud-starter-tencent-polaris-ratelimit](https://github.com/Tencent/spring-cloud-tencent/pull/413/files)

@ -17,6 +17,11 @@
package com.tencent.cloud.polaris.ratelimit.endpoint; package com.tencent.cloud.polaris.ratelimit.endpoint;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat; import com.google.protobuf.util.JsonFormat;
import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.common.util.JacksonUtils;
@ -27,17 +32,13 @@ import com.tencent.polaris.client.pb.RoutingProto;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector; import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* Endpoint of Polaris RateLimit rule. * Endpoint of Polaris RateLimit rule.
* *
@ -83,11 +84,12 @@ public class PolarisRateLimitRuleEndpoint {
String ruleJson = ""; String ruleJson = "";
try { try {
ruleJson = JsonFormat.printer().print(rule); ruleJson = JsonFormat.printer().print(rule);
} catch (InvalidProtocolBufferException e) { }
catch (InvalidProtocolBufferException e) {
LOG.error("rule to Json failed. check rule {}.", rule, e); LOG.error("rule to Json failed. check rule {}.", rule, e);
e.printStackTrace(); throw new RuntimeException("Json failed.", e);
} }
rateLimitRule.add(JacksonUtils.json2Map(ruleJson)); rateLimitRule.add(JacksonUtils.deserialize2Map(ruleJson));
} }
return rateLimitRule; return rateLimitRule;
} }

@ -17,16 +17,15 @@
package com.tencent.cloud.common.util; package com.tencent.cloud.common.util;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.io.IOException; import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
/** /**
* Utils for Jackson. * Utils for Jackson.
@ -38,7 +37,7 @@ public final class JacksonUtils {
/** /**
* Object Mapper. * Object Mapper.
*/ */
public static final ObjectMapper OM = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); public static final ObjectMapper OM = new ObjectMapper();
private static final Logger LOG = LoggerFactory.getLogger(JacksonUtils.class); private static final Logger LOG = LoggerFactory.getLogger(JacksonUtils.class);
@ -85,13 +84,4 @@ public final class JacksonUtils {
throw new RuntimeException("Json to map failed.", e); throw new RuntimeException("Json to map failed.", e);
} }
} }
public static Map<String, Object> json2Map(String jsonStr) {
try {
return OM.readValue(jsonStr, Map.class);
} catch (IOException e) {
LOG.error("Json to map failed. check if the format of the json string[{}] is correct.", jsonStr, e);
}
return new HashMap<>();
}
} }

Loading…
Cancel
Save