【问题标题】:Problem with CSV file upload with Swagger使用 Swagger 上传 CSV 文件的问题
【发布时间】:2020-10-03 22:43:09
【问题描述】:

我正在尝试使用 Swagger 2.0 实现 RESTful API,您可以使用 Java 后端上传 CSV 文件;这是 API 的 swagger 定义:

/mobileCommunications/importCallDetails:
  post:
    operationId: importCallDetails
    summary: Uploads a file that contains the call details for a specific period.
    tags:
      - MobileCommunications
    consumes:
      - multipart/form-data
    parameters:
      - in: formData
        name: upfile
        type: file
    responses:
       '200':
          description: file uploaded successfully

而且,这就是 Swagger 生成的代码:

@POST
@Path("/importCallDetails")
@Consumes({ "multipart/form-data" })
@Produces({ "application/json; charset=utf-8" })
@io.swagger.annotations.ApiOperation(value = "Uploads a file that contains the call details for a specific period.", notes = "", response = Void.class, tags={ "MobileCommunications", })
@io.swagger.annotations.ApiResponses(value = { 
    @io.swagger.annotations.ApiResponse(code = 200, message = "file uploaded successfully", response = Void.class) })
public Response importCallDetails(MultipartFormDataInput input,@Context SecurityContext securityContext,@Context Request request,@Context HttpServletRequest httpServletRequest) throws ApplicationException;

这是我的控制器代码(它根本没有执行,所以它是空的):

@Override
public Response importCallDetails(MultipartFormDataInput input, SecurityContext securityContext, Request request,
        HttpServletRequest httpServletRequest) throws ApplicationException {

    return null;
}

这是与此相关的maven依赖项:

<dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-multipart-provider</artifactId>
        <version>4.5.4.Final</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.12</version>
    </dependency>

但是,每当我尝试上传文件时,我仍然在后端收到此错误:

Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=multipart/form-data; boundary=----WebKitFormBoundaryvmfNnOPmhX557ZUx, type=interface org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput, genericType=interface org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput.

我被这个问题困扰了大约一个星期,我没有在解决方案方面取得任何进展,所以任何帮助都会在这里有所作为:)

谢谢

【问题讨论】:

标签: java file-upload swagger multipartform-data


【解决方案1】:

使用 JavaSpring + Swagger:

@PostMapping(value = "/yourPathValue", consumes = MULTIPART_FORM_DATA) // MULTIPART_FORM_DATA = "multipart/form-data"
public void yourMethodName(@RequestParam("csv") final MultipartFile csv) {
   // TODO your awesome logic
}

【讨论】:

    猜你喜欢
    • 2012-05-08
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    相关资源
    最近更新 更多