16

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
}
abaghel
  • 14,783
  • 2
  • 50
  • 66
Mayur
  • 864
  • 6
  • 14
  • 25

4 Answers4

16

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 "reportData=@mydata.json;type=application/json" -F "reportFile=@report.txt;type=text/plain"  http://localhost:8080/MyApp/lab/saveReport
abaghel
  • 14,783
  • 2
  • 50
  • 66
  • 1
    Hey I used above code and got now HTTP Status 400 - Required request part 'reportData' is not present. – Mayur Dec 03 '16 at 08:04
  • @mayur How are you sending the request? You can try using curl. I have updated my answer above with details. Please check. – abaghel Dec 03 '16 at 09:31
  • 1
    you have to dump reportData to file.json , if you are tying to send dto directly , it will give you such error. – Nasir Shah Jul 06 '21 at 05:05
3

An example of a post method which receives a json object and a generic file:

public ResponseEntity<Resource> postGenerateReport(@RequestPart GenerateReportDTO, generateReportDTO, @RequestPart MultipartFile jxhtmlReport)

For the PostMan setup (or curl or anyother REST test utility) you just have to add form-data request with 2 elements:

  1. Key:generateReportDTO, Value: File with .json extension (and compatible content with the object)
  2. Key:jxhtmlReport, Value: just any file.

Gl

Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41
1

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.

WGSSAMINTHA
  • 180
  • 1
  • 11
0

Spring Roo 2.0.0.M3 includes support for automatic scaffolding of a REST API.

For complete information, see the REST API in the reference manual.

Note the M3 version generate artifacts that could change in newer versions, so your project might not upgrade automatically if you open it with RC1 or above.

May the Force be with you.

eruiz
  • 1,963
  • 1
  • 14
  • 22