其中一个 Spring 服务器有一个用于下载文件的端点。该服务器是一个网关,因此它调用另一台服务器来获取文件。内部调用成功,文件内容返回给网关服务器。
为了保留内部调用的响应 header ,网关创建一个 ResponseEntity
对象。这些 header 包括 content-type
、content-length
和 content-disposition
。
问题是,当网关端点返回时,请求会以某种方式重定向到 /error
端点,该端点返回状态代码 406。
这是端点代码:
@GetMapping(path = "/download", produces = APPLICATION_OCTET_STREAM_VALUE)
@ResponseStatus(code = HttpStatus.OK, reason = "Success")
public ResponseEntity<byte[]> downloadAttachment(
@RequestParam String name,
@RequestParam String referenceId)
{
return internalService.downloadFile(referenceId, name);
}
最佳答案
终于找到问题了。 @ResponseStatus
注释会覆盖自定义 ResponseEntity
。事实上,这实际上记录在注释的 Javadoc 中:
The status code is applied to the HTTP response when the handler
method is invoked and overrides status information set by other means,
like {@code ResponseEntity} or {@code "redirect:"}.
我希望这能够帮助其他在同一端点上意外使用 ResponseEntity
和 @ResponseStatus
的人。
关于java - 下载文件的 Spring 端点成功(200),但调用/错误端点(406),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51868414/