天天看點

springboot 設定響應頭同時傳回byte數組

開發時,需要傳回text/javascript或者octet-stream之類的content-type相應資料。

各種使用ResponseEntity 以及 ReqMapping中的produces屬性但都是 無效,并且還伴随着各種錯誤。

可能跟項目開發時所用架構的外層設定有關。這裡記錄下自己可以使用的方法:

@GetMapping(value = "/jfqqq")
    public void tileDataCollectionFromRequest(HttpServletResponse httpResponse) {
        try {
            byte[] bytes = getBytes();
            httpResponse.setContentType("text/javascript");
            System.out.println(httpResponse.getContentType());
            ServletOutputStream outputStream = httpResponse.getOutputStream();
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
           

下面是各種失敗的測試記錄:

測試

我用網上的ResponseEntity 以及 ReqMapping中的produces屬性都試過了,如下面的幾個測試記錄,全都是傳回的content-type頭還是‘application/json;charset=UTF-8’。

并且報一些錯(下面記錄中否是在controller中使用了@Controller注解下的):

1.

@GetMapping(value = "/jfqqqq")
public byte[] tileDataCollectionFromRequest(HttpServletResponse httpResponse) {
    try {
        byte[] bytes = getBytes();
        return bytes;
    }catch (Exception e) {
        e.printStackTrace();
    }

    return new byte[0];
}
           

結果:

{
    "timestamp": 1626767562233,
    "status": 500,
    "error": "Internal Server Error",
    "message": "Unknown return value type: [B",
    "path": "/vector/realTime/181/simplify/14/13476/6219"
}
           
content-Type:application/json;charset=UTF-8
           
@GetMapping(value = "/jfqqqq/",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public byte[] tileDataCollectionFromRequest() {
        try {
            byte[] bytes = getBytes();
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new byte[0];
    }
           

結果:

{
    "timestamp": 1626767861010,
    "status": 500,
    "error": "Internal Server Error",
    "message": "Unknown return value type: [B",
    "path": "/vector/realTime/181/simplify/14/13476/6219"
}
           
content-Type:application/json;charset=UTF-8
           
@GetMapping(value = "/jfqqqq", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<byte[]> tileDataCollectionFromRequest(HttpServletResponse httpResponse) {
    try {
        byte[] bytes = getBytes();
        return ResponseEntity.ok(bytes);
    }catch (Exception e) {
        e.printStackTrace();
    }

    return ResponseEntity.ok(new byte[0]);
}
           

結果:

{
    "timestamp": 1626767963406,
    "status": 406,
    "error": "Not Acceptable",
    "message": "Could not find acceptable representation",
    "path": "/vector/realTime/181/simplify/14/13476/6219"
}
           
@GetMapping(value = "/jfqqqq")
public ResponseEntity<byte[]> tileDataCollectionFromRequest(HttpServletResponse httpResponse) {

    HttpHeaders headers = new HttpHeaders();
    headers.add("content-type",MediaType.APPLICATION_OCTET_STREAM_VALUE);
    try {
        byte[] bytes = getBytes();
        return ResponseEntity.ok().headers(headers).body(bytes);
    }catch (Exception e) {
        e.printStackTrace();
    }
    return ResponseEntity.ok().headers(headers).body(new byte[0]);
}
           

結果:

{
    "timestamp": 1626768025378,
    "status": 500,
    "error": "Internal Server Error",
    "message": "No converter for [class [B] with preset Content-Type 'application/octet-stream'",
    "path": "/vector/realTime/181/simplify/14/13476/6219"
}
           

繼續閱讀