Issue
I developed a controller which generates an excel file with a chart, but when I change the attribute "produces" from the mapping, I always get the error as mentioned in the title.
This is the header from the postman
rel="nofollow noreferrer">
And, this is the controller code
@PostMapping(value = "/exportfile/excel", produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE})
public ResponseEntity<InputStreamResource> exportExcel(@RequestBody ClientDataObjectRequest clientDataObjectRequest) {
ByteArrayInputStream file = generateCSVExcelService.generateFileDetailsWithChart(clientDataObjectRequest);
String dateFile = new SimpleDateFormat("MM-dd-yyyy").format(new java.util.Date());
String timeFile = new SimpleDateFormat("HH-mm").format(new java.util.Date());
String fileName = clientDataObjectRequest.getMetricName().replace(" ","_") + "_" + dateFile + "T" + timeFile;
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", fileName + ".xlsx");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName + ".xlsx");
//headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
return new ResponseEntity<>(new InputStreamResource(file), headers, HttpStatus.OK);
}
As you can see, I'm using the "produces" as "APPLICATION_OCTET_STREAM_VALUE" as well as the headers request and response.
What am I missing here?
EDIT: I'm receiving a request body in a Json format
{
"metricName":"Turnover Rate",
"dataFormatCodeValue": "currency",
"clientDataRequest":[
{
"clientName":"client 1",
"value":"1"
},
{
"clientName":"client 2",
"value":"2"
},
{
"clientName":"client 3",
"value":"53.78"
},
{
"clientName":"client 4",
"value":"3"
},
{
"clientName":"client 5555555",
"value":"4"
},
{
"clientName":"client 6",
"value":"33"
},
{
"clientName":"client 7",
"value":"0"
},
{
"clientName":"client 8",
"value":"8.5"
}
]
}
Solution
I found out. I had to put these bot parameters
@PostMapping(value = "/exportfile/excel", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
Answered By - dcm50
Answer Checked By - David Marino (JavaFixing Volunteer)