I need to send a file alongside with a json to my Spring Controller. I have the following controller class:
@Controller
@RequestMapping("/perform")
public class PerformController {
...
@RequestMapping(path = "gopdf", method = RequestMethod.POST, consumes = { "multipart/mixed" })
@ResponseStatus(HttpStatus.CREATED)
public void handleFileUpload(@RequestPart("file") MultipartFile file, @RequestPart("map") String map, HttpServletResponse response) throws Exception {
...
}
}
But when I curl on my server with the following command :
curl -H "Content-Type: multipart/form-data" -F "map=@map.json; type=application/json" -F "content=@SMP.docx" -X POST localhost:9000/perform/gopdf-i -v
I get 415 unsupported Media Type !
Any clue ?
The multipart/mixed for spring webflux(2.1.0) did not work for me. Here is an alternative approach that worked
It's maybe related to your request mapping annotation. I think
accept
value is missing to determine what service can accept :Example :
Import :
Documentation/API : http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/MediaType.html
The consumes thing in the other answers didn't do crap for me. The key was getting the specific multipart/* types I wanted to support onto some headers key in the RequestMapping. It was really difficult to figure out, mostly guess work and stare at the Spring source code. I'm kind-of underwhelmed with Spring's support for this, but I have managed to make it work in our Spring Boot App, but only with Tomcat?!? Something called the MultipartResolver chokes when you configure your Boot application to use Jetty...so long Jetty. But I digress...
In my Controller I set up for multipart/mixed or multipart/form-data like...
That headers thing let it uniquely identify the multipart content types it was willing to take a shot at. This lets curls like...
...or...
work.
I've found the solution: I need to use @RequestParam instead of @RequestPart