Home > database >  Spring Boot Unsupported Media Type (415) for file uploads
Spring Boot Unsupported Media Type (415) for file uploads

Time:11-03

I'm trying to create a new POST endpoint using Spring Boot using the following code:

@Controller
@Path("/my")
@MultipartConfig(maxFileSize = 1024*1024*1024, maxRequestSize = 1024*1024*1024)
public class MyResource {

    @POST
    @Path("parseFile")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "OK"),
            @ApiResponse(responseCode = "400", description = "Invalid format")})
    })
    public Response parseFile(@RequestParam("file") MultipartFile file) {
        // Use file
    }
}

I've added config in application.yml file:

spring:
  servlet:
    multipart:
      enabled: true
      max-file-size: 2MB
      file-size-threshold: 3MB

Based on the Postman Body

enter image description here

The file type that I'm trying to upload is of type *.ics and is a text file.

I'm using Spring Boot version 2.5.2.

CodePudding user response:

The error says that the media type header you send is not supported. I'm not 100% familiar @POST but I guess there is a possibility there as well.

In any cases, you can use Spring annotations. Change it from @POST and @Path to @PostMapping(path = "parseFile", consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })

CodePudding user response:

change your post method to this

 @PostMapping(value="/parsefile", consumes ="multipart/form-data")
    public Response parseFile(@RequestParam(value = "file") MultipartFile file) 
        {
            // Use file
        }
  • Related