ColdFragrance opened a new issue, #4491:
URL: https://github.com/apache/shenyu/issues/4491
### Question
I created a mvn project,add dependency in pom.xml
`
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-plugin-base</artifactId>
<version>2.5.0</version>
</dependency>
`
Add created MyRequestPlugin.java as follows
`
@Slf4j
public class MyRequestPlugin extends AbstractShenyuPlugin {
private static final Logger LOG =
LoggerFactory.getLogger(JsonOperator.class);
private static final List<HttpMessageReader<?>> MESSAGE_READERS =
HandlerStrategies.builder().build().messageReaders();
@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final
ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
FlowRuleHandler ruleHandle =
FlowRequestPluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule));
if (Objects.isNull(ruleHandle)) {
log.error("\r\nCryptor request rule configuration is null :{}",
rule.getId());
}
ServerRequest serverRequest = ServerRequest.create(exchange,
MESSAGE_READERS);
Mono<String> mono =
serverRequest.bodyToMono(String.class).switchIfEmpty(Mono.defer(() ->
Mono.just(""))).flatMap(originalBody -> {
LOG.info("get body data success data:{}", originalBody);
return Mono.just(originalBody);
});
BodyInserter<Mono<String>, ReactiveHttpOutputMessage> bodyInserter =
BodyInserters.fromPublisher(mono, String.class);
HttpHeaders headers = new HttpHeaders();
headers.putAll(exchange.getRequest().getHeaders());
headers.remove(HttpHeaders.CONTENT_LENGTH);
CachedBodyOutputMessage outputMessage = new
CachedBodyOutputMessage(exchange, headers);
return bodyInserter.insert(outputMessage, new BodyInserterContext())
.then(Mono.defer(() -> {
ServerHttpRequestDecorator decorator = new
ModifyServerHttpRequestDecorator(headers, exchange.getRequest(), outputMessage);
return
chain.execute(exchange.mutate().request(decorator).build());
})).onErrorResume((Function<Throwable, Mono<Void>>)
throwable -> release(outputMessage, throwable));
// return chain.execute(exchange);
}
}
`
then,configure
`
@Configuration
@ConditionalOnProperty(value = {"shenyu.plugins.test.enabled"}, havingValue
= "true", matchIfMissing = true)
public class Config {
@Bean
public ShenyuPlugin myRequestPlugin() {
return new MyRequestPlugin();
}
}
`
end,use
`
curl -X POST http://localhost:9077/flow/one?n=999 -H "Content-Type:
application/json" -H "X-Access-Token:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6ImFkbWluIiwiZXhwIjoxNjc4MzI0ODAyfQ.9QjlqXGYpXFXR70w3RFCFJ2dcwbrnrY2gTHyiVyF5io"
-d "{\"a\":\"1\",\"b\":\"2\",\"c\":\"3\"}"
`
Why serverRequest.bodyToMono(String.class) is empty in my code and
serverRequest.bodyToMono(String.class) is not empty in
shenyu-plugin-param-mapping?What's wrong?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]