允许两种标头内容类型-Javascript响应axios

时间:2019-08-02 14:49:48

标签: javascript java reactjs rest http-headers

我想在axios响应内容类型中接受两种内容类型,如下所示: 1)

     responseType: 'arraybuffer',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/pdf'
    }

2)

  responseType: 'application/json',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

如何组合两种报头响应类型?

当前错误:PDF下载,但没有内容。

spring boot后端::这是后端REST服务,它以字节数组的形式发送JSON或PDF文件

@PostMapping(value = "/generateBarcode")
public ResponseEntity<?> generateBarcode(@Valid @RequestBody BarCodeRequest barCodeRequest, BindingResult result) {
    if (result.hasErrors()) {
        return errorService.validationService(result); //Sends Json
    }
    return batchService.generateBarCode(barCodeRequest); //Sends PDF 
}

用于刷新PDF的实际Java代码:     公共ResponseEntity flushPDF(String batchName,byte [] baos){

    if (null != baos) {
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; " + batchName + ".pdf");
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("filename", batchName + SBGConstants.BARCODE_FILES_EXT);
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("TEST", "success");

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(baos.length)
                .contentType(MediaType.APPLICATION_PDF)
                .body(baos);
    }
    return new ResponseEntity<String>("Failed to generate barcode PDF", HttpStatus.BAD_REQUEST);
}

任何对修复响应头的想法都表示赞赏!到目前为止,我已经可以从后端实现PDF输出或JSON消息。没有结合在一起。

谢谢。

0 个答案:

没有答案