当前位置: 首页 > 知识库问答 >
问题:

使用声明性客户端Micronaut将多个文件上载到MultipartBody

柯曜文
2023-03-14

尝试使用Micronaut声明性客户端上传多个文件。

http客户端

@Client("http://localhost:8080/product")
public interface IHttpClient {
    @Post(consumes = MediaType.TEXT_PLAIN, produces = MediaType.MULTIPART_FORM_DATA)
    public String post(@Body MultipartBody file);
}

将多个文件添加到多部分

@Controller("/productManager")
public class ProductManagerController implements IProductOperation{
    private final IHttpClient iProduct;

    public ProductManagerController(IHttpClient iProduct) {
        this.iProduct = iProduct;
    }

    @Override
    public String post(CompletedFileUpload file) throws IOException {
        MultipartBody requestBody = MultipartBody.builder()
                .addPart("file", file.getFilename(), MediaType.MULTIPART_FORM_DATA_TYPE, file.getBytes())
                .addPart("file", file.getFilename(), MediaType.MULTIPART_FORM_DATA_TYPE, file.getBytes())
                .addPart("id", "asdasdsds")
                .build();
        return this.iProduct.post(requestBody);
    }
}

产品控制器

@Controller("/product")
public class ProductController implements IProductOperation {
    @Post(consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.TEXT_PLAIN)
    public String post(Publisher<CompletedFileUpload> file, String id)  {
        return null;
    }
}

例外

io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
    at io.netty.util.internal.ReferenceCountUpdater.toLiveRealRefCnt(ReferenceCountUpdater.java:74)
    at io.netty.util.internal.ReferenceCountUpdater.release(ReferenceCountUpdater.java:138)
    at io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:100)
    at io.micronaut.http.server.netty.multipart.NettyCompletedFileUpload.getBytes(NettyCompletedFileUpload.java:108)
    at com.example.ProductManagerController.post(ProductManagerController.java:26)
    at com.example.$ProductManagerControllerDefinition$$exec1.invokeInternal(Unknown Source)
    at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:146)
    at io.micronaut.context.DefaultBeanContext$4.invoke(DefaultBeanContext.java:474)
    at io.micronaut.web.router.AbstractRouteMatch.execute(AbstractRouteMatch.java:312)
    at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:118)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.lambda$buildResultEmitter$12(RoutingInBoundHandler.java:1394)
    at io.reactivex.internal.operators.flowable.FlowableDefer.subscribeActual(FlowableDefer.java:35)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14865)
    at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:57)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14865)
    at io.reactivex.internal.operators.flowable.FlowableDefer.subscribeActual(FlowableDefer.java:42)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14865)
    at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:57)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14865)
    at io.micronaut.http.server.context.ServerRequestContextFilter.lambda$doFilter$0(ServerRequestContextFilter.java:62)
    at io.reactivex.internal.operators.flowable.FlowableFromPublisher.subscribeActual(FlowableFromPublisher.java:29)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14868)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.lambda$buildExecutableRoute$8(RoutingInBoundHandler.java:1099)
    at io.micronaut.web.router.DefaultUriRouteMatch$1.execute(DefaultUriRouteMatch.java:80)
    at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:118)
    at io.micronaut.http.server.netty.RoutingInBoundHandler$1.executeRoute(RoutingInBoundHandler.java:979)
    at io.micronaut.http.server.netty.RoutingInBoundHandler$1.doOnNext(RoutingInBoundHandler.java:927)
    at io.micronaut.core.async.subscriber.CompletionAwareSubscriber.onNext(CompletionAwareSubscriber.java:52)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at io.micronaut.http.server.netty.FormDataHttpContentProcessor.onData(FormDataHttpContentProcessor.java:161)
    at io.micronaut.http.server.netty.AbstractHttpContentProcessor.doOnNext(AbstractHttpContentProcessor.java:78)
    at io.micronaut.http.server.netty.AbstractHttpContentProcessor.doOnNext(AbstractHttpContentProcessor.java:36)
    at io.micronaut.core.async.subscriber.CompletionAwareSubscriber.onNext(CompletionAwareSubscriber.java:52)
    at io.micronaut.http.netty.reactive.HandlerPublisher.publishMessage(HandlerPublisher.java:378)
    at io.micronaut.http.netty.reactive.HandlerPublisher.channelRead(HandlerPublisher.java:334)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.micronaut.http.netty.stream.HttpStreamsHandler.handleReadHttpContent(HttpStreamsHandler.java:292)
    at io.micronaut.http.netty.stream.HttpStreamsHandler.channelRead(HttpStreamsHandler.java:257)
    at io.micronaut.http.netty.stream.HttpStreamsServerHandler.channelRead(HttpStreamsServerHandler.java:121)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:93)
    at io.netty.handler.codec.http.HttpServerKeepAliveHandler.channelRead(HttpServerKeepAliveHandler.java:64)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.flow.FlowControlHandler.dequeue(FlowControlHandler.java:200)
    at io.netty.handler.flow.FlowControlHandler.read(FlowControlHandler.java:139)
    at io.netty.channel.AbstractChannelHandlerContext.invokeRead(AbstractChannelHandlerContext.java:686)
    at io.netty.channel.AbstractChannelHandlerContext.read(AbstractChannelHandlerContext.java:671)
    at io.micronaut.http.netty.reactive.HandlerPublisher.requestDemand(HandlerPublisher.java:163)
    at io.micronaut.http.netty.stream.HttpStreamsHandler$2.requestDemand(HttpStreamsHandler.java:248)
    at io.micronaut.http.netty.reactive.HandlerPublisher$ChannelSubscription.receivedDemand(HandlerPublisher.java:547)
    at io.micronaut.http.netty.reactive.HandlerPublisher$ChannelSubscription.lambda$request$0(HandlerPublisher.java:474)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:164)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:497)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)

共有1个答案

雍志新
2023-03-14

在做了一些研究之后,我们可以使用Micronaut声明性客户端实现上传多个文件,如下所示

客户端

确保客户端中的MultipartBody导入来自io。微纳特。http。客户多部分。多方主体

    @Client(id="feteBirdProduct", path = "/product")
    public interface IProductClient extends IProductOperation {
        @Post(consumes = MediaType.APPLICATION_JSON, produces = MediaType.MULTIPART_FORM_DATA)
        Maybe<HttpResponse<?>> post(@Body MultipartBody file);
    }

网关控制器

public Maybe<HttpResponse<?>> post(Publisher<CompletedFileUpload> files) {
        return Flowable.fromPublisher(files)
                .collect(MultipartBody::builder, (requestBody, file) -> {
                    requestBody
                            .addPart("file", file.getFilename(), MediaType.MULTIPART_FORM_DATA_TYPE, file.getBytes());
                })
                .flatMapMaybe(requestBody -> {
                    requestBody.addPart("id", "asdasdsds");
                    return iProductClient.post(requestBody.build());
                });
    }

用户控制器

    @Post(consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON)
    public Maybe<HttpResponse<?>> post(Publisher<CompletedFileUpload> file,String id) {
        Flowable.fromPublisher(file).subscribe(item -> {
            System.out.println("success");
        });
        return Maybe.just(HttpResponse.accepted());
    }

 类似资料:
  • 我将使用来自Micronaut文档(声明性Http客户端)的代码-我正在使用Spock 宠物手术。JAVA 我有一个声明式客户端: 我的目标是当我运行一个测试类时,我想调用(@Replaces)另一个类(PetDummy),而不是PetClient,PetDummy类位于我的测试文件夹中 测试类: 然而,在调用PetClient的最后,我也尝试了@Factory注释,但没有成功 PetClient

  • 我希望能够使用Micronaut的声明性客户端,根据我是在本地开发环境还是在生产环境中,找到不同的endpoint。 我在中设置客户端的基本uri: 阅读来自Micronaut的文档,他们让开发人员跳过了相当多的障碍,将动态值输入到实际客户端。它们实际上很混乱。所以我创建了这样的配置: 但是正如您所看到的,这实际上并不是从中读取任何值,它只是将const值设置为类的静态值。我希望值根据我所处的环境

  • Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ri

  • 我正在发送一个请求到一个服务器,它给了我一个200,没有身体(完全可以接受),但是我无法解决如何配置micronaut注释来处理这个问题。无论我尝试什么,它都试图将空响应解码为JSON(至少我认为这是它正在做的,因为它给出了一个意想不到的错误,而不是当身体出错时得到的正常杰克逊错误)。我在微型文档中找不到任何关于如何做到这一点的东西。

  • 我使用的是Jersey 2.28,想要编写一个客户端来上传1Gb的文件,而客户端JVM堆不能超过256Mb。 我在这里有什么选择? 我试着用下面的代码片段,但结果是OOM,因为Jersey似乎一直在尽可能多地从InputStream中读取数据。为了处理大量输入,可以指示球衣进行冲洗吗?

  • 我有一个基本的烧瓶应用程序上传图像文件到服务器,然后在网页上渲染。当多个客户端上传时,在当前的设计中,它们最终覆盖相同的静态资产。当在flask中处理来自多个客户端的上传时,最好的方法是什么?