系統(tǒng)使用微服務(wù)架構(gòu)阻塑,所以調(diào)用第三方接口使用RestTemplate,在報(bào)文實(shí)體和Java對(duì)象之間還需要進(jìn)行轉(zhuǎn)換果复,SpringMVC和RestTemplate提供了HttpMessageConverter作為轉(zhuǎn)換器陈莽,具體流程如下圖
HttpMessageConverter的介紹SpringMVC - HttpMessageConverter與返回JSON - 小小默:進(jìn)無止境 - 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會(huì)有多個(gè)接口將報(bào)文實(shí)體和java對(duì)象進(jìn)行轉(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 憑證有效時(shí)間走搁,單位:秒。目前是7200秒之內(nèi)的值
將返回的結(jié)果用封裝的對(duì)象接受
這里使用了FastJSON格式化屬性迈窟,但RestTemplate默認(rèn)使用Jackson私植,提供了MappingJackson2HttpMessageConverter轉(zhuǎn)換器
FastJson的注解不起作用,現(xiàn)在改用FastJson轉(zhuǎn)換器
由圖(1)可看出RestTemplate支持的轉(zhuǎn)換器釋放到數(shù)組中车酣,圖(2)可看出如果找到能處理Content-Tyep類型的轉(zhuǎn)換器并且Java實(shí)體類和Content-Tyep也對(duì)應(yīng)就會(huì)執(zhí)行read()方法并返回曲稼,隊(duì)列中剩下的轉(zhuǎn)換器不會(huì)再查找,所以用FastJson轉(zhuǎn)換器替換JackJson轉(zhuǎn)換器的思路就是把:FastJson轉(zhuǎn)換器放在數(shù)組的第一個(gè)元素骇径,這個(gè)這個(gè)執(zhí)行躯肌,jsckJson轉(zhuǎn)換器就不會(huì)再執(zhí)行
2破衔、微信接口的調(diào)用可以參考微信接口文檔getWXACodeUnlimit · 小程序
調(diào)用接口報(bào)錯(cuò)
RestTmplate默認(rèn)沒有支持image/jpge的轉(zhuǎn)換器清女,查找資料
如果直接將BufferedimagehttpMessageConverter添加到RestTemplate支持的轉(zhuǎn)換器中也不會(huì)起作用,只有是GenericHttpMessageConverter接口的實(shí)現(xiàn)類才符合要求
現(xiàn)在的解決的方法是(可能還有更優(yōu)雅的方法)晰筛,自定義一個(gè)轉(zhuǎn)換器MyHttpMessageConverter實(shí)現(xiàn)GenericHttpMessageConverter接口嫡丙,在將BufferedimagehttpMessageConverter實(shí)現(xiàn)方法復(fù)制過來
image/pg使用BufferedImage對(duì)象接受
執(zhí)行成功了