简体   繁体   English

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

[英]How to implement a download file using feign client

I have a url which downloads a file.我有一个下载文件的网址。 The signature of the url is http://services.local/api/v1/downloadFile?messageId=11090.I want to proxy it using feign client.Every time I get a exception telling my output stream is closed. url 的签名是http://services.local/api/v1/downloadFile?messageId=11090。我想使用 feign client 代理它。每次我收到一个异常告诉我的输出流关闭时。

Fri Nov 02 16:18:47 IST 2018 There was an unexpected error (type=Internal Server Error, status=500). Fri Nov 02 16:18:47 IST 2018 出现意外错误(类型=内部服务器错误,状态=500)。 Could not write JSON: getOutputStream() has already been called for this response;无法写入 JSON:已经为此响应调用了 getOutputStream(); 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"])嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException: getOutputStream() 已经为此响应调用(通过参考链: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"])

My feign client is very simple我的假客户端很简单

 @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);

I got the same problem where I want to make API call from one Microservice to another Microservice so I mapped that API which returning byte[] .我遇到了同样的问题,我想从一个微服务到另一个微服务进行 API 调用,所以我映射了返回byte[] API。

So your code should be like:所以你的代码应该是这样的:

@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);

     :
     :
}

It will return downloaded file in byte[] .它将在byte[]返回下载的文件。

Note: Your query parameter will be messageId as par given example.注意:您的查询参数将是messageId作为标准示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM