Spring WebClient : No suitable writer found for part: param1 - spring-boot

I am trying to upload a file with some other text attributes using Spring 5 Webclient. However I am getting an error for the same: Below mentioned is the code:
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder.part("myFile", uploadedFile.getBytes());
bodyBuilder.part("param1", formType, MediaType.TEXT_PLAIN).header("Content-Disposition", "form-data; name=param1").header("Content-type", "text/plain");
bodyBuilder.part("param2", orgId, MediaType.TEXT_PLAIN).header("Content-Disposition", "form-data; name=param2").header("Content-type", "text/plain");
bodyBuilder.part("param3", ignoreCheck, MediaType.TEXT_PLAIN).header("Content-Disposition", "form-data; name=param3").header("Content-type", "text/plain");
bodyBuilder.part("param4", forSentiLink, MediaType.TEXT_PLAIN).header("Content-Disposition", "form-data; name=param4").header("Content-type", "text/plain");
return webclient.post()
.uri(myUri)
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(bodyBuilder.build()))
.exchange()
.flatMap(response -> response.bodyToMono(MyDto.class))
.flux()
.blockFirst();
This code is used to create a form-data with some textual fields and a file field.
But I get an error for it when i try to run the code:
Resolved [org.springframework.core.codec.CodecException: No suitable writer found for part: param1]
org.springframework.core.codec.CodecException: No suitable writer found for part: param1
at org.springframework.http.codec.multipart.MultipartHttpMessageWriter.encodePart(MultipartHttpMessageWriter.java:299)
at org.springframework.http.codec.multipart.MultipartHttpMessageWriter.lambda$encodePartValues$4(MultipartHttpMessageWriter.java:252)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:368)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:212)
at reactor.core.publisher.FluxOnAssembly$OnAssemblySubscriber.onSubscribe(FluxOnAssembly.java:432)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:139)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:63)
at reactor.core.publisher.FluxOnAssembly.subscribe(FluxOnAssembly.java:122)
at reactor.core.publisher.FluxConcatMap.subscribe(FluxConcatMap.java:121)
at reactor.core.publisher.FluxOnAssembly.subscribe(FluxOnAssembly.java:122)
at reactor.core.publisher.Flux.subscribe(Flux.java:7938)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:442)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:212)
at reactor.core.publisher.FluxOnAssembly$OnAssemblySubscriber.onSubscribe(FluxOnAssembly.java:432)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:139)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:63)
at reactor.core.publisher.FluxOnAssembly.subscribe(FluxOnAssembly.java:122)
at reactor.core.publisher.FluxConcatMap.subscribe(FluxConcatMap.java:121)
at reactor.core.publisher.FluxOnAssembly.subscribe(FluxOnAssembly.java:122)
at reactor.core.publisher.Flux.subscribe(Flux.java:7938)
at reactor.core.publisher.FluxConcatArray$ConcatArraySubscriber.onComplete(FluxConcatArray.java:207)
at reactor.core.publisher.FluxConcatArray.subscribe(FluxConcatArray.java:80)
at reactor.core.publisher.FluxOnAssembly.subscribe(FluxOnAssembly.java:117)
at reactor.core.publisher.FluxContextStart.subscribe(FluxContextStart.java:49)
at reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63)
at reactor.core.publisher.FluxOnAssembly.subscribe(FluxOnAssembly.java:122)
at reactor.core.publisher.Flux.subscribe(Flux.java:7938)
at reactor.netty.channel.MonoSendMany.subscribe(MonoSendMany.java:80)
at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:153)
at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)
at reactor.core.publisher.MonoOnAssembly.subscribe(MonoOnAssembly.java:61)
at reactor.core.publisher.Mono.subscribe(Mono.java:3877)
at reactor.core.publisher.FluxConcatIterable$ConcatIterableSubscriber.onComplete(FluxConcatIterable.java:146)
at reactor.core.publisher.FluxConcatIterable.subscribe(FluxConcatIterable.java:60)
at reactor.core.publisher.FluxOnAssembly.subscribe(FluxOnAssembly.java:122)
at reactor.core.publisher.MonoIgnoreElements.subscribe(MonoIgnoreElements.java:37)
at reactor.core.publisher.MonoOnAssembly.subscribe(MonoOnAssembly.java:61)
at reactor.core.publisher.Mono.subscribe(Mono.java:3877)
at reactor.core.publisher.FluxFlatMap.trySubscribeScalarMap(FluxFlatMap.java:200)
at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:53)
at reactor.core.publisher.MonoOnAssembly.subscribe(MonoOnAssembly.java:61)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
at reactor.core.publisher.MonoOnAssembly.subscribe(MonoOnAssembly.java:61)
at reactor.netty.http.client.HttpClientConnect$HttpIOHandlerObserver.onStateChange(HttpClientConnect.java:438)
at reactor.netty.ReactorNetty$CompositeConnectionObserver.onStateChange(ReactorNetty.java:492)
at reactor.netty.resources.PooledConnectionProvider$DisposableAcquire.onStateChange(PooledConnectionProvider.java:503)
at reactor.netty.resources.PooledConnectionProvider$PooledConnection.onStateChange(PooledConnectionProvider.java:442)
at reactor.netty.channel.ChannelOperationsHandler.channelActive(ChannelOperationsHandler.java:60)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:225)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:211)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:204)
at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelActive(CombinedChannelDuplexHandler.java:414)
at io.netty.channel.ChannelInboundHandlerAdapter.channelActive(ChannelInboundHandlerAdapter.java:69)
at io.netty.channel.CombinedChannelDuplexHandler.channelActive(CombinedChannelDuplexHandler.java:213)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:225)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:211)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:204)
at reactor.netty.tcp.SslProvider$SslReadHandler.userEventTriggered(SslProvider.java:731)
at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:341)
at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:327)
at io.netty.channel.AbstractChannelHandlerContext.fireUserEventTriggered(AbstractChannelHandlerContext.java:319)
at io.netty.handler.logging.LoggingHandler.userEventTriggered(LoggingHandler.java:183)
at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:341)
at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:327)
at io.netty.channel.AbstractChannelHandlerContext.fireUserEventTriggered(AbstractChannelHandlerContext.java:319)
at io.netty.handler.ssl.SslHandler.setHandshakeSuccess(SslHandler.java:1753)
at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1412)
at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1227)
at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1274)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:503)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:442)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:281)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1422)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:931)
at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:792)
at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:502)
at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:407)
at io.netty.util.concurrent.SingleThreadEventExecutor$6.run(SingleThreadEventExecutor.java:1050)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
The server side for this API is python, where it tries to get the file from request:
files={'files': open('file.txt','rb')}
values={'key' : 'value'}
r=requests.post(url,files=files,data=values)
{"status": 400, "message": "extra keys not allowed # data[u'param1']", "meta": {"status": 400, "msg": "extra keys not allowed # data[u'param1']""response": null}

String header1 = String.format("form-data; name=%s; filename=%s", "attach", myFile.getOriginalName());
bodyBuilder.part("attach", new ByteArrayResource(myFile.getBytes())).header("Content-Disposition", header1);
bodyBuilder.part("param1", param1);
bodyBuilder.part("param2", param2);
bodyBuilder.part("param3", param3);
bodyBuilder.part("param4", param4);
String respons = restClientService.getWebClient()
.post().uri(myuri)
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(bodyBuilder.build()))
.exchange()
.flatMap(response -> response.bodyToMono(MyDto.class))
.flux()
.blockFirst();

Related

Spring cloud gateway - io.netty.util.internal.OutOfDirectMemoryError when using modifyRequestBody filter

I am using
Spring Cloud Gateway version 3.0.3
reactor-netty-core 1.0.10
and i am facing a io.netty.util.internal.OutOfDirectMemoryError when the spring cloud gateway modifyRequestBody filter is being used. When it is being commented out, load tests runs fine without any error.
In our filters, we are doing a cache of the request body which will be used in our Fallback when there is a timeout or errors. Cache is just using the default ServerWebExchangeUtils.cacheRequestBody method.Fallback is implemented using Resilience4j Circuit Breaker.
In part of our chain, we are also using the spring gateway modifyRequestBody filter to modify our request body before sending it to the resource server.
OUT 2022-01-19 18:10:54.344 ERROR 16 --- [r-http-epoll-12] io.netty.util.ResourceLeakDetector : LEAK: ByteBuf.release() was not called before it's garbage-collected. See https://netty.io/wiki/reference-counted-objects.html for more information.
OUT Recent access records:
OUT Created at:
OUT io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:402)
OUT io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:187)
OUT io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:178)
OUT io.netty.buffer.AbstractByteBufAllocator.buffer(AbstractByteBufAllocator.java:115)
OUT org.springframework.core.io.buffer.NettyDataBufferFactory.allocateBuffer(NettyDataBufferFactory.java:71)
OUT org.springframework.core.io.buffer.NettyDataBufferFactory.allocateBuffer(NettyDataBufferFactory.java:39)
OUT org.springframework.core.codec.CharSequenceEncoder.encodeValue(CharSequenceEncoder.java:91)
OUT org.springframework.core.codec.CharSequenceEncoder.lambda$encode$0(CharSequenceEncoder.java:75)
OUT reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106)
OUT reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:74)
OUT reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1816)
OUT reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:151)
OUT reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
OUT reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
OUT reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:127)
OUT reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onNext(FluxContextWrite.java:107)
OUT reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onNext(FluxMapFuseable.java:295)
OUT reactor.core.publisher.FluxFilterFuseable$FilterFuseableConditionalSubscriber.onNext(FluxFilterFuseable.java:337)
OUT reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1816)
OUT reactor.core.publisher.MonoCollect$CollectSubscriber.onComplete(MonoCollect.java:159)
OUT reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1817)
OUT reactor.core.publisher.FluxCallable.subscribe(FluxCallable.java:49)
OUT reactor.core.publisher.Mono.subscribe(Mono.java:4338)
OUT reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.subscribeNext(MonoIgnoreThen.java:255)
OUT reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:51)
OUT reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64)
OUT reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
OUT reactor.core.publisher.Mono.subscribe(Mono.java:4338)
OUT reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.subscribeNext(MonoIgnoreThen.java:255)
OUT reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:51)
OUT reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
OUT reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
OUT reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64)
OUT reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
OUT reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
OUT reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:157)
OUT reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:74)
OUT reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:120)
OUT reactor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onNext(FluxDefaultIfEmpty.java:101)
OUT reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onNext(FluxContextWrite.java:107)
OUT reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onNext(FluxMapFuseable.java:295)
OUT reactor.core.publisher.FluxFilterFuseable$FilterFuseableConditionalSubscriber.onNext(FluxFilterFuseable.java:337)
OUT reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1816)
OUT reactor.core.publisher.MonoCollect$CollectSubscriber.onComplete(MonoCollect.java:159)
OUT reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142)
OUT reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:260)
OUT reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142)
OUT reactor.netty.channel.FluxReceive.terminateReceiver(FluxReceive.java:468)
OUT reactor.netty.channel.FluxReceive.drainReceiver(FluxReceive.java:260)
OUT reactor.netty.channel.FluxReceive.request(FluxReceive.java:129)
OUT reactor.core.publisher.FluxMap$MapSubscriber.request(FluxMap.java:162)
OUT reactor.core.publisher.FluxPeek$PeekSubscriber.request(FluxPeek.java:138)
OUT reactor.core.publisher.FluxMap$MapSubscriber.request(FluxMap.java:162)
OUT reactor.core.publisher.MonoCollect$CollectSubscriber.onSubscribe(MonoCollect.java:103)
OUT reactor.core.publisher.FluxMap$MapSubscriber.onSubscribe(FluxMap.java:92)
OUT reactor.core.publisher.FluxPeek$PeekSubscriber.onSubscribe(FluxPeek.java:171)
OUT reactor.core.publisher.FluxMap$MapSubscriber.onSubscribe(FluxMap.java:92)
OUT reactor.netty.channel.FluxReceive.startReceiver(FluxReceive.java:167)
OUT reactor.netty.channel.FluxReceive.lambda$subscribe$2(FluxReceive.java:146)
OUT io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
OUT io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469)
OUT io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:384)
OUT io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
OUT io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
OUT io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
OUT java.lang.Thread.run(Thread.java:748)
Netty.docs: Reference counted objects
Sample Code Snippet:
return predicateSpec
.path(pathPattern)
.filters(f -> f
.filter(cachingRequestBodyFilter)
.modifyRequestBody(String.class, String.class, (exchange, modified) -> {
//Modify request body method here
})
.circuitBreaker(c -> c.setName("sessionTimeoutCircuitBreaker").setFallbackUri("forward:/timeoutFallback/"+country+"/"+domain))
)
.uri(uri);

Error while connecting to Spring Boot RSocket server from RSocket-Java Client

I am having issue while connecting to Spring Boot RSocket application over TCP. The client when using RSocketRequester works fine but when I try to connect using RSocketFactory client it keep getting errors. Code below.
RSocket rSocket = this.client = RSocketFactory
.connect()
.mimeType(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.toString(), MediaType.APPLICATION_JSON_VALUE)
.frameDecoder(PayloadDecoder.ZERO_COPY)
.transport(TcpClientTransport.create("localhost", 7000))
.start()
.block();
Flux<Payload> s = rSocket.requestStream(DefaultPayload.create("1234", "socket"));
s.subscribe();
This gives error as below:
java.lang.IndexOutOfBoundsException: readerIndex(1) + length(115) exceeds writerIndex(6): AbstractPooledDerivedByteBuf$PooledNonRetainedSlicedByteBuf(ridx: 1, widx: 6, cap: 6/6, unwrapped: PooledUnsafeDirectByteBuf(ridx: 27, widx: 27, cap: 1024))
at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1477)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1463)
at io.netty.buffer.AbstractByteBuf.readSlice(AbstractByteBuf.java:880)
at io.rsocket.metadata.TaggingMetadata$1.next(TaggingMetadata.java:47)
at io.rsocket.metadata.TaggingMetadata$1.next(TaggingMetadata.java:37)
at org.springframework.messaging.rsocket.DefaultMetadataExtractor.extractEntry(DefaultMetadataExtractor.java:136)
at org.springframework.messaging.rsocket.DefaultMetadataExtractor.extract(DefaultMetadataExtractor.java:119)
at org.springframework.messaging.rsocket.annotation.support.MessagingRSocket.createHeaders(MessagingRSocket.java:195)
at org.springframework.messaging.rsocket.annotation.support.MessagingRSocket.handleAndReply(MessagingRSocket.java:167)
at org.springframework.messaging.rsocket.annotation.support.MessagingRSocket.requestStream(MessagingRSocket.java:127)
at io.rsocket.RSocketResponder.requestStream(RSocketResponder.java:207)
at io.rsocket.RSocketResponder.handleFrame(RSocketResponder.java:310)
at reactor.core.publisher.LambdaSubscriber.onNext(LambdaSubscriber.java:160)
at reactor.core.publisher.MonoFlatMapMany$FlatMapManyInner.onNext(MonoFlatMapMany.java:242)
at reactor.core.publisher.FluxGroupBy$UnicastGroupedFlux.drainRegular(FluxGroupBy.java:554)
at reactor.core.publisher.FluxGroupBy$UnicastGroupedFlux.drain(FluxGroupBy.java:630)
at reactor.core.publisher.FluxGroupBy$UnicastGroupedFlux.subscribe(FluxGroupBy.java:696)
at reactor.core.publisher.Flux.subscribe(Flux.java:8174)
at reactor.core.publisher.MonoFlatMapMany$FlatMapManyMain.onNext(MonoFlatMapMany.java:188)
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1637)
at reactor.core.publisher.MonoProcessor.onNext(MonoProcessor.java:317)
at io.rsocket.internal.ClientServerInputMultiplexer.lambda$new$1(ClientServerInputMultiplexer.java:116)
at reactor.core.publisher.LambdaSubscriber.onNext(LambdaSubscriber.java:160)
at reactor.core.publisher.FluxGroupBy$GroupByMain.drainLoop(FluxGroupBy.java:380)
at reactor.core.publisher.FluxGroupBy$GroupByMain.drain(FluxGroupBy.java:316)
at reactor.core.publisher.FluxGroupBy$GroupByMain.onNext(FluxGroupBy.java:201)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:114)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:114)
at reactor.netty.channel.FluxReceive.drainReceiver(FluxReceive.java:218)
at reactor.netty.channel.FluxReceive.onInboundNext(FluxReceive.java:351)
at reactor.netty.channel.ChannelOperations.onInboundNext(ChannelOperations.java:348)
at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:90)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:321)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:295)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:830)
This particular error is as I understand is because of netty message wrapping (from other threads on stackoverflow) but how to solve it?
The server is Spring Boot 5+ RSocket but the client is only using RSocket-Java.
The problem is inside mime types.
In your case server awaits CBOR but you proceed application/json
The code solution: change the way of RSocketRequester initialization as at the example below and you client with be sending CBOR you can see at by enabling debug: logging.level.io.rsocket.FrameLogger: DEBUG. That's all for hello world, no need custom strategies or factories implementations on the client-side
#Bean
RSocketRequester rSocketRequester(RSocketStrategies strategies) {
return RSocketRequester
.builder()
.rsocketStrategies(strategies)
.connectTcp("127.0.0.1", 7000)
.retry(5)
.block();
}
Btw, I didn't reach the solution with JSON on both side even with custom Encoder & Decoder on both side. I guess the reason here is that there is no CBOR to Jackson converter and only vice versa: org.springframework.http.codec.cbor.Jackson2CborEncoder
From this, use the following to generate metadata.
CompositeByteBuf metadata = ByteBufAllocator.DEFAULT.compositeBuffer();
RoutingMetadata routingMetadata = TaggingMetadataCodec.createRoutingMetadata(ByteBufAllocator.DEFAULT, List.of("/route"));
CompositeMetadataCodec.encodeAndAddMetadata(metadata,
ByteBufAllocator.DEFAULT,
WellKnownMimeType.MESSAGE_RSOCKET_ROUTING,
routingMetadata.getContent());

java.util.NoSuchElementException: No value present even though we use stream

I am new to java, i am running a local server (jar) and trying to hit an API endpoint. All data is correct, and I am getting below :
java.util.NoSuchElementException: No value present
The execption seems to be occuring on the below line in the code snippet
.get()
any help is greatly appreciated!!
Using java8
final List<BundleValue> existingBundleValues = bundles.stream()
.filter(bundle -> bundle.type().equals(bundleType))
.findFirst()
.get()
.values()
.stream()
.map(BundleValue::convertFromAbstractBundleValue)
.collect(Collectors.toList());
java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135)
at com.groupon.goods.productbundlingservice.service.BundlingService.isNewBundle(BundlingService.java:105)
at com.groupon.goods.productbundlingservice.resources.ProductBundlingServiceResource.createBundle(ProductBundlingServiceResource.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
at io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650)
at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:34)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:45)
at io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:39)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at com.groupon.jtier.bundles.ctx.CtxFilter.doFilter(CtxFilter.java:56)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at com.groupon.jtier.bundles.debug.StenoLoggerFilter.doFilter(StenoLoggerFilter.java:118)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at com.groupon.jtier.bundles.debug.RequestServletFilter.doFilter(RequestServletFilter.java:46)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:241)
at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:52)
at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:455)
at io.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:68)
at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:169)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:561)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:334)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:679)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:597)
at java.lang.Thread.run(Thread.java:748)
firstFirst returns an Optional, that you call get on, if that is an empty Optional - you will get that Exception.
You could write it as follows, where if it is present (that Optional) you would map it to Map::values (I assume that your findFirst returns some kind of a Map), otherwise get a Collections.emptyList().
bundles.stream()
.filter(bundle -> bundle.type().equals(bundleType))
.findFirst()
.map(Map::values)
.orElse(Collections.emptyList())
.stream()
.map(BundleValue::convertFromAbstractBundleValue)
.collect(Collectors.toList());
Optional#get() throws NoSuchElementException if no value is present means value is null.
To avoid exception you can use Optional#orElse(T other) which returns the value if present, otherwise returns other value passed in the method.
Optional<Integer> d1 = Optional.of(10);
Optional<Integer> d2 = Optional.empty();
Integer res1 = d1.orElse(0); // return 10 since value is present
Integer res2 = d2.orElse(0); // return 0 since value is not present
Or you want to throw an exception when if no value is present then use orElseThrow
optionalObj.orElseThrow(ArithmeticException::new)
Then you can do further operation based on value.

Aws integration spring: Cannot download a file within a folder

The below code is trying downloaded a single file from within a folder on S3. It is using the message handler below.
NOTE: The code below works for a file that is placed within the root directory of the S3 bucket. It does not work for a file placed in a folder.
#Bean
#ServiceActivator(inputChannel = "s3Channel")
public MessageHandler s3Handler() {
ExpressionParser PARSER = new SpelExpressionParser();
S3MessageHandler s3MessageHandler = new S3MessageHandler(amazonS3(), "test.queue", true);
s3MessageHandler.setOutputChannel(this.channel);
s3MessageHandler.setCommand(S3MessageHandler.Command.DOWNLOAD);
s3MessageHandler.setKeyExpression(PARSER.parseExpression("payload instanceof T(java.io.File) ? payload.name : headers.key"));
s3MessageHandler.setObjectAclExpression(new ValueExpression<>(CannedAccessControlList.PublicReadWrite));
return s3MessageHandler;
}
Using the payload:
Message<?> payload = MessageBuilder.withPayload(new File("test/s3testfile.xml")).build();
this.s3ProcessChannel.send(payload);
The following stack trace occurs downloading a file from with a folder.
org.springframework.messaging.MessageHandlingException: error occurred in message handler [s3MessageHandler]; nested exception is com.amazonaws.services.s3.model.AmazonS3Exception: Not Found (Service: Amazon S3; Status Code: 404; Error Code: 404 Not Found;)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:139) ~[spring-integration-core-4.2.5.RELEASE.jar:na]
at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:135) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.messaging.support.ExecutorSubscribableChannel.sendInternal(ExecutorSubscribableChannel.java:91) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:117) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:104) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at com.rapid7.live.assessment.aws.inbound.channels.SqsAssessmentMessageChannel.send(SqsAssessmentMessageChannel.java:44) ~[classes/:na]
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.integration.endpoint.MessageProducerSupport.sendMessage(MessageProducerSupport.java:105) ~[spring-integration-core-4.2.5.RELEASE.jar:na]
at org.springframework.integration.aws.inbound.SqsMessageDrivenChannelAdapter.access$400(SqsMessageDrivenChannelAdapter.java:53) ~[spring-integration-aws-1.0.0.M1.jar:na]
at org.springframework.integration.aws.inbound.SqsMessageDrivenChannelAdapter$IntegrationQueueMessageHandler.handleMessageInternal(SqsMessageDrivenChannelAdapter.java:154) ~[spring-integration-aws-1.0.0.M1.jar:na]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:389) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.executeMessage(SimpleMessageListenerContainer.java:181) ~[spring-cloud-aws-messaging-1.1.0.RC1.jar:1.1.0.RC1]
at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer$MessageExecutor.run(SimpleMessageListenerContainer.java:314) ~[spring-cloud-aws-messaging-1.1.0.RC1.jar:1.1.0.RC1]
at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer$SignalExecutingRunnable.run(SimpleMessageListenerContainer.java:368) [spring-cloud-aws-messaging-1.1.0.RC1.jar:1.1.0.RC1]
at org.springframework.core.task.SimpleAsyncTaskExecutor$ConcurrencyThrottlingRunnable.run(SimpleAsyncTaskExecutor.java:251) [spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
The file does exist in the correct bucket.
Well, the problem is here that File.getName() returns only file name, without any parent dirs.
Consider change your keyExpression to the :
"payload instanceof T(java.io.File) ? payload.path : headers.key"
Or just don't reply on the File payload but always specify the proper key in the headers.

Header Enricher for spring xd to call http-client t pass headers

I am using http header enricher component to pass the headers but I am not able to make a http request using http-client
I have a stream like below I am trying to make a http rest Post request and log it to the logger.I have a API-Key header for which I used this below header enricher module
https://github.com/spring-projects/spring-xd-modules/tree/master/header-enricher
stream create --name test24 --definition "http |header-enricher --headers={\"Api-Key\":\"'xyz'\"}| http-client --url='''http://xyz:/abc''' --httpMethod=POST | log " --deploy
http post --data '{"profileReferenceID":"pPPpe85Ht91e%2FpCvlkJMkniwiM%2BGvPjATkjc6HGqaJFS065txVj%2BhqDPsHz54KEm1RrCXftjNCSl2fpBKKKdrie9n6t9jetWhk29ELCmbw0%3D","templateURL":{"type":"type","href":"/content/notification/templates/EOSWEL?firstName=Abcd&userName=dddd&accountLast=2144&phone=1234&SvcgLOBCd=MS","method":"POST"}}'
> POST (text/plain;Charset=UTF-8) http://localhost:9000 {"profileReferenceID":"pPPpe85Ht91e%2FpCvlkJMkniwiM%2BGvPjATkjc6HGqaJFS065txVj%2BhqDPsHz54KEm1RrCXftjNCSl2fpBKKKdrie9n6t9jetWhk29ELCmbw0%3D","templateURL":{"type":"type","href":"/content/notification/templates/EOSWEL?firstName=Abcd&userName=dddd&accountLast=2144&phone=1234&SvcgLOBCd=MS","method":"POST"}}
2015-11-16T13:05:24-0600 1.2.1.RELEASE WARN xdbus.test24.1-1 retry.RejectAndDontRequeueRecoverer - Retries exhausted for message (Body:'{"profileReferenceID":"pPPpe85Ht91e%2FpCvlkJMkniwiM%2BGvPjATkjc6HGqaJFS065txVj%2BhqDPsHz54KEm1RrCXftjNCSl2fpBKKKdrie9n6t9jetWhk29ELCmbw0%3D","templateURL":{"type":"type","href":"/content/notification/templates/EOSWEL?firstName=Abcd&userName=dddd&accountLast=2144&phone=1234&SvcgLOBCd=MS","method":"POST"}}'MessageProperties [headers={requestMethod=POST, User-Agent=Java/1.8.0_60, Host=localhost:9000, Content-Length=305, contentType=text/plain, Api-Key=xyz, requestPath=/, originalContentType=text/plain;Charset=UTF-8}, timestamp=null, messageId=null, userId=null, appId=null, clusterId=null, type=null, correlationId=null, replyTo=null, contentType=text/plain, contentEncoding=null, contentLength=0, deliveryMode=null, expiration=null, priority=0, redelivered=false, receivedExchange=, receivedRoutingKey=xdbus.test24.1, deliveryTag=2, messageCount=0])
I am getting 403 forbidden i get this error from my webservice if my client api key is not passed which i was able to reproduce in post man .So I assume my API-Key header is not passed.
caused by: org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://xyx/abc]; nested exception is org.springframework.web.client.HttpClientErrorException: 403 Forbidden
at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:410) ~[spring-integration-http-4.1.6.RELEASE.jar:na]
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:99) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:287) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:245) ~[spring-integration-core-4.1.6.RELEASE.jar:na]
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) ~[spring-messaging-4.1.7.RELEASE.jar:4.1.7.RELEASE]
You should add your Api-Key header to be mapped to the HTTP request header:
http-client --url='''http://xyz:/abc''' --httpMethod=POST --mappedRequestHeaders=HTTP_REQUEST_HEADERS,Api-Key
mappedRequestHeaders
request message header names to be propagated to/from the adpater/gateway (String, default: HTTP_REQUEST_HEADERS)
Where HTTP_REQUEST_HEADERS is a set of only standard HTTP headers which are appropriate for request.
http://docs.spring.io/spring-xd/docs/current/reference/html/#http-client

Resources