什么是render函数?什么时候会用到render函数?

600 条最强 Linux 命令总结

  返回  

springboot 设置响应头

2021/7/20 16:18:53 浏览:

开发时,需要返回text/javascript或者octet-stream之类的content-type相应数据。
然后springboot返回的content-type:都是json。
下面是结论和测试用例。

结果

    @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"
}

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号