gpt4 book ai didi

java - 使用 swagger inflector 和 dropwizard 上传文件

转载 作者:行者123 更新时间:2023-12-04 15:07:28 25 4
gpt4 key购买 nike

我为我的 Java API 使用 swagger-inflector v.1.0.17。这是我构建 API 的来源:https://github.com/swagger-api/swagger-samples/tree/master/java/inflector-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'

还有我在 Controller 中的方法:

public ResponseContext uploadLogo(RequestContext context, java.io.InputStream file) {
ResponseContext response = new ResponseContext().contentType(MediaType.APPLICATION_JSON_TYPE);
System.out.println("in here");
}

我正在尝试 CURL:

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

我使用的是 swagger-inflector 1.0.17 版。知道我做错了什么吗?

最佳答案

看起来正在调用 uploadLogo 而不是使用带有 java.io.File 参数的 java.io.InputStream,对应框架用于支持文件上传的临时文件。

该方法在SwaggerOperationControllerapply方法中通过反射调用。执行此调用时,因为参数不同 - 您的方法签名定义了一个 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 ...;
}

请注意参数类型的变化。

关于java - 使用 swagger inflector 和 dropwizard 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65851440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com