question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support @RequestPart annotation

See original GitHub issue

TLDR: @RequestPart parameters should have paramType of form and models should be generated for @RequestPart annotated parameters.


When using the @RequestPart annotation, the paramType attribute for that parameter is incorrect (unless the parameter is a MultipartFile). When making an api operation that uses a file upload, we can no longer use @RequestBody, instead it must be @RequestPart, which means the overall request Content-Type becomes mulitpart/form-data.

While this all works fine if the if the file upload is the only parameter, once you try to add additional parameters (i.e. pieces that will be included in its own boundary), it no longer works (at least not with swagger-ui, because the spec is incorrect).

The other issue is the parameters annotated as @RequestPart do not have their models added to the generated spec files.

So given the following example, swagger-ui should create a form that takes a text input and file input (which it does), however the request parameter is using paramType ‘body’, and when the form is submitted, only the file gets submitted in the request.

@ApiOperation(value = "")
@RequestMapping(value = "/issue", method = RequestMethod.POST)
@ResponseBody
public MyResponse issueRequest(
        @ApiParam(required = true) @RequestPart MyRequest request,
        @ApiParam(required = true) @RequestPart MultipartFile document)

Using the custom annotation readers, I am able to work around the paramType issue, but not adding the model to the spec issue.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
spasariacommented, Jun 14, 2022

Someone solved the problem described by @ndorigatti? i have the same scenario and I don’t know how to fix it

0reactions
ndorigatticommented, Dec 14, 2021

Hello, i see again this issue in springfox-boot-starter:3.0.0

I have an endpoint with both Pojo and Multipart file. I can successfully call the endpoint with Postman tool if I set it: @PostMapping(value = "/sito", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) public void insertSito(@RequestPart(name = "edifici", required = false) MultipartFile file, @RequestPart(name="sito") CreateSitoDTO creationRequest) {

But swagger shows ONLY the file part (that is the only optional one btw…). If i switch the json part to @RequestBody, swagger shows both the elements, but fails to call the endpoint, what i get is: org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver: Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;boundary=----WebKitFormBoundaryr6riTHMZjZyrkhQI;charset=UTF-8' not supported]

I’m currently using spring-boot 2.5.7 and springfox-boot-starter 3.0.0 (NOT snapshot)

Read more comments on GitHub >

github_iconTop Results From Across the Web

RequestPart (Spring Framework 6.0.2 API)
Annotation that can be used to associate the part of a "multipart/form-data" request with a method argument. Supported method argument types include ...
Read more >
Multipart Request Handling in Spring - Baeldung
Using @RequestPart. This annotation associates a part of a multipart request with the method argument, which is useful for sending complex ...
Read more >
RequestPart with mixed multipart request, Spring MVC 3.2
annotation.RequestPart; It is used as Combining @RequestBody and file upload. Using @RequestParam like this @RequestParam("file") MultipartFile file you can ...
Read more >
RequestBody and Multipart on Spring Boot - Perficient Blogs
Create SpringBoot Project Creating Model, Controller and Service class... Test the API using Postman ... So, annotation should be altered.
Read more >
OpenAPI for Spring MVC: support @RequestPart : IDEA-266645
OpenAPI tab for this mapping ignores the @RequestPart - annotated parameters: /rest4/p2: post: summary: "POST rest4/p2" operationId: "create" responses: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found