1

I have created a rest API to accept MULTIPART_FORM_DATA as below. But once I hit the service using Postman, I am getting HTTP Status 415 – Unsupported Media Type exception

@POST
@Path("/fileupload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public String uploadfile(@RequestParam(value = "file") MultipartFile file) {

    System.out.println(file.getName());

    return "Success String";
}

What is wrong here? To consume MediaType.MULTIPART_FORM_DATA, do I need to make any modifications? In Postman I have attached a text file in the BODY and hit the endpoint. The content type is set as "multipart/form-data"

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
User_1940878
  • 321
  • 1
  • 6
  • 25
  • please follow this link: https://stackoverflow.com/questions/16015548/tool-for-sending-multipart-form-data-request It says to remove the content-type from postman. – Brooklyn99 Sep 17 '19 at 17:38

3 Answers3

1

Seems like you are confused with Spring rest API with Rest easy implementation.

  1. In Resteasy, Normal way to handle uploaded file is via MultipartFormDataInput or Map uploaded file to a POJO class via @MultipartForm

https://www.mkyong.com/webservices/jax-rs/file-upload-example-in-resteasy/

How to POST a multipart/form data with files programatically in a REST API

  1. If you want to use spring rest approach, refer here Multipart File upload Spring Boot
Ast
  • 143
  • 7
0

Have a look on below tutorial on uploading file in spring boot

https://devkonline.com/tutorials/content/ANGULAR-8-SPRING-BOOT-FILE-UPLOAD

0

You have probably imported different annotations. Try it this way

import org.springframework.web.bind.annotation.*;

import static org.springframework.http.MediaType.*;

@PostMapping(value = "/fileupload", consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_VALUE)
public String uploadfile(@RequestParam(value = "file") MultipartFile file) {
    System.out.println(file.getName());
    return "Success String";
}
jpact
  • 1,042
  • 10
  • 23