format code

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

@ -39,5 +39,4 @@
- [docs:optimize example](https://github.com/Tencent/spring-cloud-tencent/pull/385)
- [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)
- [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)
- [test: add PostInitPolarisSDKContextTest](https://github.com/Tencent/spring-cloud-tencent/pull/397)

@ -17,6 +17,11 @@
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.util.JsonFormat;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.lang.Nullable;
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.
*
@ -83,11 +84,12 @@ public class PolarisRateLimitRuleEndpoint {
String ruleJson = "";
try {
ruleJson = JsonFormat.printer().print(rule);
} catch (InvalidProtocolBufferException e) {
}
catch (InvalidProtocolBufferException 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;
}

@ -17,16 +17,15 @@
package com.tencent.cloud.common.util;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.util.StringUtils;
/**
* Utils for Jackson.
@ -38,7 +37,7 @@ public final class JacksonUtils {
/**
* 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);
@ -85,13 +84,4 @@ public final class JacksonUtils {
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