webFlux是真鸡巴复杂. 成功读取到body.

This commit is contained in:
eson 2019-07-09 03:08:10 +08:00
parent dba005c31b
commit ee69aa0a6d

View File

@ -3,6 +3,7 @@ package cn.ecpark.service.usergw.biz.filters.factory;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -14,14 +15,20 @@ import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.support.DefaultServerRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
import lombok.extern.slf4j.Slf4j;
@ -58,9 +65,36 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
GenericService gs = gsPool.get(uri);
ServerRequest serverRequest = new DefaultServerRequest(exchange);
Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).map(s -> {
log.info("original text:{}", s);
/**
* do your decrypt() here and convert to String.class
* or you can change the output class to change format
*/
return "foooooooo " + s;
});
modifiedBody.subscribe( databuferr -> {
log.info(databuferr);
});
ServerHttpRequest req = exchange.getRequest();
Flux<DataBuffer> body = req.getBody();
body.subscribe( databuferr -> {
byte[] bytes = new byte[databuferr.readableByteCount()];
databuferr.read(bytes);
log.warn(bytes.toString());
});
body.flatMap( databuferr->{
byte[] bytes = new byte[databuferr.readableByteCount()];
databuferr.read(bytes);
log.warn(bytes.toString());
return Flux.empty();
} );
MultiValueMap<String, String> queryParams = req.getQueryParams();