feign响应Image流对象
feign面向方法签名的http调用,越来越受欢迎,类型于rpc的thrift,只需要关注方法签名和返回值即可,当然feign响应流对象时,需要我们做一下特殊处理,否则会出现异常。也有一些文章通过重写HttpMessageConvert来实现,但我测试后发现还是失败的。
> Accept: image/*会返回406 Not Acceptable
### 解决方法
feign代码,注意返回值必须是`feign.Response`
```
@PostMapping(value = "/wxa/getwxacodeunlimit")
feign.Response getQrImagePath(@RequestParam("access_token") String accessToken,@RequestBody Map<String, Object> body);
class WeixinClientFallback implements WeixinClient {
```
restcontroller代码,需要显示声明`produces`类型,这里因为是图片,所以声明为`MediaType.IMAGE_JPEG_VALUE`
```
@GetMapping(value = "/v1/api/sales/qrcode", produces = MediaType.IMAGE_JPEG_VALUE)
public void qrcode(HttpServletResponse response) {
//获取token
Map<String, Object> tokenMap = weixinClient.getToken("client_credential", wxAppId, wxAppSecret);
if (tokenMap == null) {
throw Exceptions.paramError("获取token失败");
}
if (tokenMap.containsKey("errcode")) {
throw Exceptions.paramError(MapUtils.getString(tokenMap, "errmsg"));
}
String scene = String.format("salesPersonId=%s&agencyId=%s",
TokenContext.getToken().getUserId(),
TokenContext.getToken().getCompanyId());
String token = MapUtils.getString(tokenMap, "access_token");
Map<String, Object> requestParams = new HashMap<>();
requestParams.put("scene", scene);
requestParams.put("page", weixinPage);
feign.Response imgReponse = weixinClient.getQrImagePath(token, requestParams);
if (imgReponse == null) {
throw Exceptions.paramError("获取二维码失败");
}
try (InputStream inputStream = imgReponse.body().asInputStream();
ServletOutputStream outputStream = response.getOutputStream()) {
response.setContentType("image/png");
outputStream.write(IOUtils.toByteArray(inputStream));
} catch (RuntimeException e) {
throw Exceptions.paramError("获取二维码失败");
} catch (Exception ex) {
throw Exceptions.paramError("获取二维码失败");
}
}
```
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2015-08-05 AngularJS~集成的ajax和服务的注入
2014-08-05 说说设计模式~装饰器模式(Decorator)
2014-08-05 说说设计模式~观察者模式(Observer)
2013-08-05 我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器