diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/command/UpdateGatewayApiDefinitionGroupCommandHandler.java b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/command/UpdateGatewayApiDefinitionGroupCommandHandler.java index 963561f8..cc655962 100644 --- a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/command/UpdateGatewayApiDefinitionGroupCommandHandler.java +++ b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/command/UpdateGatewayApiDefinitionGroupCommandHandler.java @@ -17,9 +17,11 @@ package com.alibaba.csp.sentinel.adapter.gateway.common.command; import java.net.URLDecoder; import java.util.HashSet; -import java.util.List; +import java.util.Set; import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; +import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem; +import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem; import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager; import com.alibaba.csp.sentinel.command.CommandHandler; import com.alibaba.csp.sentinel.command.CommandRequest; @@ -27,7 +29,9 @@ import com.alibaba.csp.sentinel.command.CommandResponse; import com.alibaba.csp.sentinel.command.annotation.CommandMapping; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.util.StringUtil; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; /** * @author Eric Zhao @@ -52,10 +56,36 @@ public class UpdateGatewayApiDefinitionGroupCommandHandler implements CommandHan RecordLog.info("[API Server] Receiving data change (type: gateway API definition): {0}", data); String result = SUCCESS_MSG; - List apiDefinitions = JSONArray.parseArray(data, ApiDefinition.class); - GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>(apiDefinitions)); + + Set apiDefinitions = parseJson(data); + GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions); + return CommandResponse.ofSuccess(result); } private static final String SUCCESS_MSG = "success"; + + /** + * Parse json data to set of {@link ApiDefinition}. + * + * Since the predicateItems of {@link ApiDefinition} is set of interface, + * here we parse predicateItems to {@link ApiPathPredicateItem} temporarily. + */ + private Set parseJson(String data) { + Set apiDefinitions = new HashSet<>(); + JSONArray array = JSON.parseArray(data); + for (Object obj : array) { + JSONObject o = (JSONObject)obj; + ApiDefinition apiDefinition = new ApiDefinition((o.getString("apiName"))); + Set predicateItems = new HashSet<>(); + JSONArray itemArray = o.getJSONArray("predicateItems"); + if (itemArray != null) { + predicateItems.addAll(itemArray.toJavaList(ApiPathPredicateItem.class)); + } + apiDefinition.setPredicateItems(predicateItems); + apiDefinitions.add(apiDefinition); + } + + return apiDefinitions; + } }