Add configuration to control whether to write null value in JSON serialization in aggregation #283

This commit is contained in:
Francis Dong
2021-08-23 16:29:50 +08:00
committed by dxfeng10
parent f898b7145c
commit 8d68b737bd
4 changed files with 28 additions and 13 deletions

View File

@@ -101,4 +101,8 @@ refresh-local-cache:
gateway-group-enabled: true gateway-group-enabled: true
app-auth-enabled: true app-auth-enabled: true
flow-control-rule-enabled: true flow-control-rule-enabled: true
rpc-service-enabled: true rpc-service-enabled: true
fizz:
aggregate:
writeMapNullValue: false

View File

@@ -358,6 +358,8 @@ public class MapUtil {
} }
return newValue; return newValue;
}); });
} else {
target.put(key, value);
} }
}); });
return target; return target;

View File

@@ -17,12 +17,19 @@
package we.filter; 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.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; 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.ServerWebExchange;
import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain; 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.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers; import reactor.core.scheduler.Schedulers;
@@ -46,19 +57,10 @@ import we.fizz.Pipeline;
import we.fizz.input.Input; import we.fizz.input.Input;
import we.flume.clients.log4j2appender.LogService; import we.flume.clients.log4j2appender.LogService;
import we.plugin.auth.ApiConfig; import we.plugin.auth.ApiConfig;
import we.spring.http.server.reactive.ext.FizzServerHttpRequestDecorator;
import we.util.MapUtil; import we.util.MapUtil;
import we.util.NettyDataBufferUtils; import we.util.NettyDataBufferUtils;
import we.util.WebUtils; 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 * @author Francis Dong
*/ */
@@ -185,7 +187,11 @@ public class AggregateFilter implements WebFilter {
if(aggResult.getBody() instanceof String) { if(aggResult.getBody() instanceof String) {
jsonString = (String) aggResult.getBody(); jsonString = (String) aggResult.getBody();
}else { }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); LOGGER.debug("response body: {}", jsonString);
if (aggResult.getHeaders() != null && !aggResult.getHeaders().isEmpty()) { if (aggResult.getHeaders() != null && !aggResult.getHeaders().isEmpty()) {

View File

@@ -33,4 +33,7 @@ public class AggregateFilterProperties {
@Value("${need-auth:true}") @Value("${need-auth:true}")
private boolean needAuth; private boolean needAuth;
@Value("${fizz.aggregate.writeMapNullValue:false}")
private boolean writeMapNullValue;
} }