springboot 设置响应头同时返回byte数组

开发时,需要返回text/javascript或者octet-stream之类的content-type相应数据。 各种使用ResponseEntity 以及 ReqMapping中的produces属性但都是 无效,并且还伴随着各种错误。 可能跟项目开发时所用框架的外层设置有关。这里记录下自己可以使用的方法:

下面是各种失败的测试记录:

测试

我用网上的ResponseEntity 以及 ReqMapping中的produces属性都试过了,如下面的几个测试记录,全都是返回的content-type头还是‘application/json;charset=UTF-8’。 并且报一些错(下面记录中否是在controller中使用了@Controller注解下的): 1.

结果:

{
    "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

结果:

{
    "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

结果:

{
    "timestamp": 1626767963406,
    "status": 406,
    "error": "Not Acceptable",
    "message": "Could not find acceptable representation",
    "path": "/vector/realTime/181/simplify/14/13476/6219"
}

结果:

{
    "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"
}
经验分享 程序员 微信小程序 职场和发展