系統(tǒng)使用微服務(wù)架構(gòu),所以調(diào)用第三方接口使用RestTemplate,在報文實體和Java對象之間還需要進行轉(zhuǎn)換,SpringMVC和RestTemplate提供了HttpMessageConverter作為轉(zhuǎn)換器,具體流程如下圖

HttpMessageConverter的介紹SpringMVC - HttpMessageConverter與返回JSON - 小小默:進無止境 - CSDN博客服務(wù)器返回的可能有多種類型(application/json, application/atom+xml, application/x-www-form-urlencoded, application/octet-stream, application/pdf, application/rss+xml, application/xhtml+xml, application/xml, text/event-stream, text/html, text/markdown, text/plain, text/xml, application/*+json, image/vnd.wap.wbmp, image/png, image/x-png, image/jpeg, image/bmp, image/gif),所以轉(zhuǎn)換器接口HttpMessageConverter會有多個接口將報文實體和java對象進行轉(zhuǎn)換

調(diào)用微信應(yīng)用接口接口大概的流程
1、獲取接口調(diào)用憑證
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}
如果調(diào)用成功返回 {"access_token?":"fsfnesf5f88sfs558w1212fsf5524fs22fs","expires_in","7200"}
access_token 接口調(diào)用憑證
expires_in 憑證有效時間,單位:秒。目前是7200秒之內(nèi)的值
將返回的結(jié)果用封裝的對象接受


這里使用了FastJSON格式化屬性,但RestTemplate默認使用Jackson,提供了MappingJackson2HttpMessageConverter轉(zhuǎn)換器


FastJson的注解不起作用,現(xiàn)在改用FastJson轉(zhuǎn)換器

由圖(1)可看出RestTemplate支持的轉(zhuǎn)換器釋放到數(shù)組中,圖(2)可看出如果找到能處理Content-Tyep類型的轉(zhuǎn)換器并且Java實體類和Content-Tyep也對應(yīng)就會執(zhí)行read()方法并返回,隊列中剩下的轉(zhuǎn)換器不會再查找,所以用FastJson轉(zhuǎn)換器替換JackJson轉(zhuǎn)換器的思路就是把:FastJson轉(zhuǎn)換器放在數(shù)組的第一個元素,這個這個執(zhí)行,jsckJson轉(zhuǎn)換器就不會再執(zhí)行



2、微信接口的調(diào)用可以參考微信接口文檔getWXACodeUnlimit · 小程序
調(diào)用接口報錯



RestTmplate默認沒有支持image/jpge的轉(zhuǎn)換器,查找資料

如果直接將BufferedimagehttpMessageConverter添加到RestTemplate支持的轉(zhuǎn)換器中也不會起作用,只有是GenericHttpMessageConverter接口的實現(xiàn)類才符合要求


現(xiàn)在的解決的方法是(可能還有更優(yōu)雅的方法),自定義一個轉(zhuǎn)換器MyHttpMessageConverter實現(xiàn)GenericHttpMessageConverter接口,在將BufferedimagehttpMessageConverter實現(xiàn)方法復制過來


image/pg使用BufferedImage對象接受

執(zhí)行成功了
