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

使用swagger Findector和dropwizard上传文件

越麒
2023-03-14

我在我的Java API中使用了swagger-inflector V.1.0.17。以下是我构建API的基础:https://github.com/swagger-api/swagger-samples/tree/master/java/fintector-dropwizard-guice

我的文件上传API定义如下:

'/orders-logo':
   post:
     x-swagger-router-controller: OrdersController
     tags:
       - orders
     summary: This API is upload a logo
     operationId: uploadLogo
     consumes:
       - multipart/form-data
     produces:
       - application/json
     parameters:
       - name: file
         in: formData
         description: file to upload
         required: false
         type: file
     responses:
       '200':
         description: Success
         schema:
           $ref: '#/definitions/TokenView'
       '400':
         description: The request doesn't comply with the schema specified
         schema:
           $ref: '#/definitions/ApiError'
       '401':
         description: Missing or invalid Authorization header
         schema:
           $ref: '#/definitions/ApiError'
       '500':
         description: Internal server error while processing the request
         schema:
           $ref: '#/definitions/ApiError'

和控制器中的方法:

public ResponseContext uploadLogo(RequestContext context, java.io.InputStream file) {
    ResponseContext response = new ResponseContext().contentType(MediaType.APPLICATION_JSON_TYPE);
    System.out.println("in here");
}
curl -i -X POST \
-H "Content-Type: multipart/form-data" \
-H 'Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhbm9uXzU3MjBmMGUzLTMxMzItNDQ1OC1hYjYyLTBmY2EwMDEyMjAyNCIsImlzcyI6IkNsZXZlcmdpZnQiLCJleHAiOjE2MTY1MjEwMjMsImJhc2ljIjoidHJ1ZSIsIm1lcmNoYW50IjoiaGlnaHN0cmVldHZvdWNoZXJzIn0.s-yukQincZoWevQARLNrl57SABAetBxMCOF39RSd7a7tEnds5pIgo5ekRTQgX98MlEXFyDoHe0ws7iMIo_-cOQ' \
-H 'Api-Key: 7a34526370e2c3d4f2784f874aacbc0b' \
-F "file=@download.jpeg" "http://localhost:8080/v1/orders-logo"
{"code":500,"message":"There was an error processing your request. It has been logged (ID: bad38eb089e2a71a)"}
com.clevergift.dropwizard.InflectorExceptionMapper: There was an error processing your request. It has been logged (ID: bad38eb089e2a71a)
! java.lang.IllegalArgumentException: argument type mismatch
! at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
! at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
! at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
! at java.base/java.lang.reflect.Method.invoke(Method.java:566)
! at io.swagger.inflector.controllers.SwaggerOperationController.apply(SwaggerOperationController.java:503)
! ... 67 common frames omitted
! Causing: io.swagger.inflector.utils.ApiException: There was an error processing your request. It has been logged (ID: bad38eb089e2a71a)
! at io.swagger.inflector.controllers.SwaggerOperationController.apply(SwaggerOperationController.java:560)
! at io.swagger.inflector.controllers.SwaggerOperationController.apply(SwaggerOperationController.java:89)
! at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
! at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
! at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
! at java.base/java.lang.reflect.Method.invoke(Method.java:566)
! 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:1623)
! at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:35)
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
! 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:1610)
! at com.clevergift.filters.ApiKeyFilter.doFilter(ApiKeyFilter.java:99)
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
! at com.clevergift.filters.CORSFilter.doFilter(CORSFilter.java:15)
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
! at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:89)
! at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:121)
! at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:133)
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
! at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:540)
! at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
! at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1345)
! at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
! at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:480)
! at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
! at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1247)
! at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
! at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:239)
! at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:52)
! at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:703)
! at io.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:67)
! at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:56)
! at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:174)
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
! at org.eclipse.jetty.server.Server.handle(Server.java:505)
! at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:370)
! at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:267)
! at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
! at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
! at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117)
! at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:698)
! at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:804)
! at java.base/java.lang.Thread.run(Thread.java:834)
0:0:0:0:0:0:0:1 - - [25/Jan/2021:16:12:03 +0000] "POST /v1/orders-logo HTTP/1.1" 500 110 "-" "curl/7.64.1" 5

共有1个答案

澹台星光
2023-03-14

看起来像是在调用uploadlogo,而不是用java.io.inputstreamjava.io.file参数调用,该参数对应于框架用于支持文件上传的临时文件。

此方法由SwagagerOperationControllerApply方法中的反射调用。当执行此调用时,由于参数不同-您的方法签名定义java.io.InputStream,但框架传递了java.io.file作为参数-您将获得指示的异常:

java.lang.IllegalArgumentException: argument type mismatch

请尝试以下签名:

public ResponseContext uploadLogo(RequestContext context, java.io.File file) {
    ResponseContext response = new ResponseContext().contentType(MediaType.APPLICATION_JSON_TYPE);
    System.out.println("in here");
    // The rest of your code
    return ...;
}
 类似资料:
  • 我必须从我的网站上传一个文件,但cnt似乎可以使用drop wizard。 这是我网站上的表格。 如何在后端接收文件? 解决办法是

  • 我正在使用dropwizard,我想一次上传多个文件。 我如何改变我的代码来上传多个文件? 我正在使用<code>org.glassfish.jersey。“媒体”,“泽西媒体多部分”,“2.17”用于文件上传。 这是我上传单个文件的代码:

  • 有人能告诉我如何在dropwizard 1.2.6中实现可恢复的文件上传吗?因此,如果用户试图上传一个大文件(4-5 GB),如果出现网络故障或浏览器错误关闭,那么用户将能够从中断的地方恢复该过程。

  • 我是Dropwizard的新手。我想在我的项目中实现AWS S3文件上传服务。 我没有收到任何通过拖放在AWS S3上上传文件的教程。 我在pom.xml中添加了以下依赖项 我已经注册了多部分功能。将应用程序类的run()方法中的- 然后在资源类中定义方法为- 但在运行时,它显示以下日志:- 如何获取上传文件的url?如何通过编码检查文件上传?我遗漏了什么吗?有人知道这件事吗。如果dropwiza

  • 这段javascript代码如下所示。注意,当file input元素的值发生变化时(即,一旦他们选择了要上传的文件),就会调用该命令 请原谅所有控制台日志。它的目的是显示输出和调试。 您将看到,我还附加了一个username字段,只是为了测试帖子是否在控制器中使用了一些完整的数据。的确是。以下是记录数组的输出: 有什么想法吗??

  • 我已成功将图像文件上载到。但是我在使用MockMvc测试时遇到了一个问题。当我运行测试用例时,我发现异常文件未找到,访问被拒绝。 控制器看起来像这样: 我的测试用例如下所示: 我的jsp文件如下所示: