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"

1

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.