From 8d68b737bd1a7065ccbe6f313e3af60f70742dad Mon Sep 17 00:00:00 2001 From: Francis Dong Date: Mon, 23 Aug 2021 16:29:50 +0800 Subject: [PATCH] Add configuration to control whether to write null value in JSON serialization in aggregation #283 --- .../src/main/resources/application.yml | 6 +++- .../src/main/java/we/util/MapUtil.java | 2 ++ .../main/java/we/filter/AggregateFilter.java | 30 +++++++++++-------- .../we/filter/AggregateFilterProperties.java | 3 ++ 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/fizz-bootstrap/src/main/resources/application.yml b/fizz-bootstrap/src/main/resources/application.yml index 38793bf..de9adfd 100644 --- a/fizz-bootstrap/src/main/resources/application.yml +++ b/fizz-bootstrap/src/main/resources/application.yml @@ -101,4 +101,8 @@ refresh-local-cache: gateway-group-enabled: true app-auth-enabled: true flow-control-rule-enabled: true - rpc-service-enabled: true \ No newline at end of file + rpc-service-enabled: true + +fizz: + aggregate: + writeMapNullValue: false \ No newline at end of file diff --git a/fizz-common/src/main/java/we/util/MapUtil.java b/fizz-common/src/main/java/we/util/MapUtil.java index c25e2f4..0347697 100644 --- a/fizz-common/src/main/java/we/util/MapUtil.java +++ b/fizz-common/src/main/java/we/util/MapUtil.java @@ -358,6 +358,8 @@ public class MapUtil { } return newValue; }); + } else { + target.put(key, value); } }); return target; diff --git a/fizz-core/src/main/java/we/filter/AggregateFilter.java b/fizz-core/src/main/java/we/filter/AggregateFilter.java index 25f3a6a..6b34cab 100644 --- a/fizz-core/src/main/java/we/filter/AggregateFilter.java +++ b/fizz-core/src/main/java/we/filter/AggregateFilter.java @@ -17,12 +17,19 @@ package we.filter; -import com.alibaba.fastjson.JSON; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import javax.annotation.Resource; + import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; -import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -34,6 +41,10 @@ import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebFilterChain; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; + import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; @@ -46,19 +57,10 @@ import we.fizz.Pipeline; import we.fizz.input.Input; import we.flume.clients.log4j2appender.LogService; import we.plugin.auth.ApiConfig; -import we.spring.http.server.reactive.ext.FizzServerHttpRequestDecorator; import we.util.MapUtil; import we.util.NettyDataBufferUtils; import we.util.WebUtils; -import javax.annotation.Resource; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - /** * @author Francis Dong */ @@ -185,7 +187,11 @@ public class AggregateFilter implements WebFilter { if(aggResult.getBody() instanceof String) { jsonString = (String) aggResult.getBody(); }else { - jsonString = JSON.toJSONString(aggResult.getBody()); + if (this.aggregateFilterProperties.isWriteMapNullValue()) { + jsonString = JSON.toJSONString(aggResult.getBody(), SerializerFeature.WriteMapNullValue); + } else { + jsonString = JSON.toJSONString(aggResult.getBody()); + } } LOGGER.debug("response body: {}", jsonString); if (aggResult.getHeaders() != null && !aggResult.getHeaders().isEmpty()) { diff --git a/fizz-core/src/main/java/we/filter/AggregateFilterProperties.java b/fizz-core/src/main/java/we/filter/AggregateFilterProperties.java index 0bc7b88..9b3cb4d 100644 --- a/fizz-core/src/main/java/we/filter/AggregateFilterProperties.java +++ b/fizz-core/src/main/java/we/filter/AggregateFilterProperties.java @@ -33,4 +33,7 @@ public class AggregateFilterProperties { @Value("${need-auth:true}") private boolean needAuth; + + @Value("${fizz.aggregate.writeMapNullValue:false}") + private boolean writeMapNullValue; }