gpt4 book ai didi

java - 如何使用feign客户端实现下载文件

转载 作者:行者123 更新时间:2023-11-30 05:56:15 26 4
gpt4 key购买 nike

我有一个下载文件的网址。 url的签名是http://services.local/api/v1/downloadFile?messageId=11090.I想要使用 feign 客户端代理它。每次我收到一个异常,告诉我的输出流已关闭。

Fri Nov 02 16:18:47 IST 2018 There was an unexpected error (type=Internal Server Error, status=500). Could not write JSON: getOutputStream() has already been called for this response; nested exception is com.fasterxml.jackson.databind.JsonMappingException: getOutputStream() has already been called for this response (through reference chain: org.springframework.security.web.firewall.FirewalledResponse["response"]->org.springframework.security.web.header.HeaderWriterFilter$HeaderWriterResponse["response"]->org.springframework.security.web.context.HttpSessionSecurityContextRepository$SaveToSessionResponseWrapper["response"]->org.springframework.security.web.firewall.FirewalledResponse["response"]->org.apache.catalina.connector.ResponseFacade["writer"])

我的 feign 客户端非常简单

 @FeignClient(name = "downloadAPI", url = "${service.ip}")
public interface DownloadApiProxy {

@RequestMapping(method = RequestMethod.GET, value = "/downloadFile")
public void downloadFile(HttpServletResponse response,
@RequestParam(value = "downloadMessageId", required = false) String messageId);

最佳答案

我遇到了同样的问题,我想从一个微服务到另一个微服务进行 API 调用,因此我映射了返回 byte[] 的 API。

所以你的代码应该是这样的:

@FeignClient(name = "downloadAPI", url = "${service.ip}")
public interface DownloadApiProxy {

@RequestMapping(method = RequestMethod.GET, value = "/downloadFile")
public byte[] downloadFile(HttpServletResponse response, @RequestParam(value = "messageId", required = false) String messageId);

:
:
}

它将返回byte[]中下载的文件。

注意:您的查询参数将是 messageId,如给定示例所示。

关于java - 如何使用feign客户端实现下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53117281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com