Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use multipart and @RequestBody together in spring?

I want to create a API which can have parameter as multipart file and JSON object (@RequestBody). Please find following snippet while calling this API. I am getting HTTP 415 Unsupported Media Type error. If I remove @RequestBody LabPatientInfo reportData then it works fine.

@RequestMapping(value={"/lab/saveReport"}, method={RequestMethod.POST}, 
                consumes={"multipart/form-data"}, headers={"Accept=application/json"})
@ResponseBody
public ResponseEntity<String>
saveReport(@RequestParam(value="reportFile") MultipartFile reportFile,
           @RequestBody LabPatientInfo reportData) throws IOException {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    logger.info("in Lab Save Report");
    logger.info("Report Data {} ", reportData);
    //logger.info("Request BODY {} ", request.getAttribute("data"));
    return new ResponseEntity<String>(HttpStatus.OK);
}

following is LabPatientInfo class.

@RooJson(deepSerialize = true)
@RooToString
public class LabPatientInfo {
    
    private String firstName;
    private String phoneNumber;
    private String DateOfBirth;
    private Integer age;
    private String gender;
    private String refferedBy; 
    private String reportfile;
    private String reportType;
    private String reportDate;
    private String purpose;
    private String followUpDate;
    private List<ReportDataInfo> analytes;

while hitting API I am passing following JSON object with uploaded file..

{
    "firstName":"abc",
    "phoneNumber":"898989",
    "DateOfBirth":"asas",
    "age":"asas",
    "gender":"asas",
    "refferedBy":"asas",
    "reportfile":"asas",
    "reportType":"asas",
    "reportDate":"asas",
    "purpose":"asas",
    "followUpDate":"asas",
    "analytes":null
}
like image 624
Mayur Avatar asked Dec 02 '16 04:12

Mayur


People also ask

How do I create a multipart file with request body?

To pass the Json and Multipart in the POST method we need to mention our content type in the consume part. And we need to pass the given parameter as User and Multipart file. Here, make sure we can pass only String + file not POJO + file. Then convert the String to Json using ObjectMapper in Service layer.

Can we use @RequestBody with get in spring boot?

@RequestBody: Annotation is used to get request body in the incoming request. Note: First we need to establish the spring application in our project. Step 2: Click on Generate which will download the starter project.

What is the use of @RequestBody annotation in spring?

Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.

Can we use @RequestBody in Spring MVC?

The @RequestBody annotation is applicable to handler methods of Spring controllers. This annotation indicates that Spring should deserialize a request body into an object. This object is passed as a handler method parameter.


2 Answers

You can use @RequestPart like below. This will support both json object and multipart file.

@ResponseBody
public ResponseEntity<String>
saveReport(@RequestPart (value="reportFile") MultipartFile reportFile,
           @RequestPart LabPatientInfo reportData) throws IOException {

In order to test it using curl you can create one file for your json part (reportData). Say for example you create "mydata.json" file and paste your json payload in it. And say your reportFile is "report.txt". Now you can send request using curl like below.

curl -v -H "Content-Type:multipart/form-data" -F "[email protected];type=application/json" -F "[email protected];type=text/plain"  http://localhost:8080/MyApp/lab/saveReport
like image 52
abaghel Avatar answered Sep 28 '22 23:09

abaghel


When a parameter is annotated with @RequestPart the content of the part is passed through an HttpMessageConverter to resolve the method argument with the 'Content-Type' of the request part in mind. This is analogous to what @RequestBody does to resolve an argument based on the content of a regular request.

so, we can parse @Requestbody as @RequestPart as "abaghel" and reportData need to be a json file.

like image 39
WGSSAMINTHA Avatar answered Sep 28 '22 23:09

WGSSAMINTHA