通过 REST 控制器上传文本文件

Uploading text file trougth REST controller

我尝试将 form/multipart-data 发送到我的其余控制器端点,但它总是抛出错误。

我认为最重要的是错误行:

Required argument [CompletedFileUpload uploadFiles] not specified

我在控制台中看到文件中的文本已传递,但方法参数不满足。

邮递员的测试请求:

curl -X POST \
  http://localhost:8080/v1/staff/code \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJXQUs4ODY2NDQyMjAwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjI1MTYyMzkwMjJ9.7l_WP6tYT3hv-oQc4iIboFUqStjbP04WiyHPYJ-EqYc' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Length: 260' \
  -H 'Content-Type: multipart/form-data; boundary=--------------------------826814601374767521347273' \
  -H 'Host: localhost:8080' \
  -H 'Postman-Token: 0817c2fb-066b-45ad-96fe-b5cbce33f562,379d26dc-e4d3-48a4-abf7-aefec85a4dea' \
  -H 'User-Agent: PostmanRuntime/7.17.1' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F emails=@/C:/Users/ondra/Desktop/emails.txt

我的休息控制器

 @Post(consumes = MediaType.MULTIPART_FORM_DATA)
 public HttpResponse sendCodesToEmails(CompletedFileUpload uploadFile) {}

来自控制台的日志:

16:23:07.060 [nioEventLoopGroup-1-17] DEBUG i.m.h.server.netty.NettyHttpServer - Server localhost:8080 Received Request: POST /v1/staff/code
16:23:07.061 [nioEventLoopGroup-1-17] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Matching route POST - /v1/staff/code
16:23:07.061 [nioEventLoopGroup-1-17] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Matched route POST - /v1/staff/code to controller class com.zentity.wakanda.users.codes.api.$StaffCodeApiDefinition$Intercepted
16:23:07.061 [nioEventLoopGroup-1-17] TRACE i.m.h.n.reactive.HandlerPublisher - HandlerPublisher (state: IDLE) received demand: 1
16:23:07.061 [nioEventLoopGroup-1-17] TRACE i.m.h.n.reactive.HandlerPublisher - Demand received for next message (state = DEMANDING). Calling context.read()
16:23:07.062 [nioEventLoopGroup-1-17] TRACE i.m.h.n.reactive.HandlerPublisher - HandlerPublisher (state: DEMANDING) emitting next message: ----------------------------503586631991435845838300
Content-Disposition: form-data; name="emails"; filename="emails.txt"
Content-Type: text/plain

ondrej.schrek@gmail.com
ondrej.schrek@zentity.com
----------------------------503586631991435845838300--

16:23:07.062 [nioEventLoopGroup-1-17] TRACE i.m.h.s.netty.RoutingInBoundHandler - Received HTTP Data for request [POST /v1/staff/code]: content-disposition: form-data; name="emails"; filename="emails.txt"
content-type: text/plain; charset=UTF-8
content-length: 50
Completed: true
IsInMemory: true

16:23:07.064 [pool-1-thread-24] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Found matching exception handler for exception [Required argument [CompletedFileUpload uploadFiles] not specified]: io.micronaut.web.router.StatusRouteMatch@5fdda10c

参数名称必须与部分名称匹配,否则使用@Part注释。

CompletedFileUpload uploadFile -> CompletedFileUpload emails

@Part("emails") CompletedFileUpload uploadFile