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

问题描述 投票:1回答:1

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

Fri Nov 02 16:18:47 IST 2018出现意外错误(类型=内部服务器错误,状态= 500)。无法写入JSON:已经为此响应调用了getOutputStream();嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:已经为此响应调用了getOutputStream()(通过引用链:org.springframework.security.web.firewall.FirewalledResponse [“response”] - > org.springframework。 security.web.header.HeaderWriterFilter $ HeaderWriterResponse [ “反应”] - > org.springframework.security.web.context.HttpSessionSecurityContextRepository $ SaveToSessionResponseWrapper [ “反应”] - > org.springframework.security.web.firewall.FirewalledResponse [“响应“] - > org.apache.catalina.connector.ResponseFacade [” 作家“])

我的假装客户很简单

 @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);
java spring spring-cloud spring-cloud-feign
1个回答
0
投票

我遇到了同样的问题,我想从一个微服务到另一个微服务进行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作为参考示例。

© www.soinside.com 2019 - 2024. All rights reserved.