java 获取“不支持的媒体类型”错误415(八位字节流)

pprl5pva  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(127)

我开发了一个控制器,它生成一个带有图表的excel文件,但是当我从Map中更改属性"produces"时,我总是得到标题中提到的错误。
这是 Postman

的邮件头
这是控制器代码

@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);
    }

正如您所看到的,我使用的"products"是"APPLICATION_OCET_STREAM_VALUE"以及头文件request和response。
我错过了什么?
编辑:我收到一个Json格式的请求主体

{
    "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"
       }
    ]
}
wrrgggsh

wrrgggsh1#

我发现了。我不得不把这两个参数放入@PostMapping中:producesconsumes

@PostMapping(value = "/exportfile/excel", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)

相关问题