開發問題:postman調試正常,在項目使用restTemplate發送就是不成功.


背景:使用微信公眾號接口,生成帶參數的二維碼.

沖突:postman調試正常,在項目使用restTemplate發送就是不成功.

問題1:可能是url不對。

問題2:可能請求的json對象,格式不對。

問題3:restTemplate,json解析不對。

解決1:

postman : https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=
項目:public static final String POST_CREATE_QRCODE_URL="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={access_token}";
結果1:結果一致.

解決2:

postman:
項目日志:
結果2:完全一致.

解決3:打開springBoot的restTemplate日志設置成debug.


發送請求:
日志如下:

參數信息:{"action_info":{"scene":{"scene_id":1,"scene_str":"856b8ffb-a6ca-4bc9-813b-1419a6530ac6"}},"action_name":"QR_SCENE","expire_seconds":300}                                                                                          
參數信息:{"action_info":{"scene":{"scene_str":"856b8ffb-a6ca-4bc9-813b-1419a6530ac6","scene_id":1}},"action_name":"QR_SCENE","expire_seconds":300}                                                                                          
HTTP POST https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=32_ZTBg5kzh3hUMoaP7xJik8YAFJkoO5gNwjosG1XvI2b4MORGLNitSV4RQSF2JIOx_4MrDGZ5sbNq8bEJ4miwOfK2mcO64TyMIweEUOR5IsY49Kl2vsQuq1plmS2eBQbhzHZVK1HOzFvh125jPPLJhAJALHF    
Accept=[application/xml, text/xml, application/json, application/*+xml, application/*+json]                                                                                                                                             
Writing [{"action_info":{"scene":{"scene_str":"856b8ffb-a6ca-4bc9-813b-1419a6530ac6","scene_id":1}},"action_name":"QR_SCENE","expire_seconds":300}] with org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter  
Response 200 OK                                                                                                                                                                                                                         
Reading to [com.alibaba.fastjson.JSONObject]                                                                                                                                                                                            
返回結果:{"errcode":40052,"errmsg":"invalid action name hint: [PQ4c.a00331960]"}                                                                                                                                                            

關鍵信息:org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter ??參數轉換使用的xml轉換器.
接下來定位到:參數轉換問題

查看源代碼:

啊啊啊啊啊,返回的是Object對象.
查看其他方法:

修改為toJSONString做測試
日志如下:

參數信息:{"action_info":{"scene":{"scene_id":1,"scene_str":"323a31ec-906a-4cb2-a24f-fb846fb16997"}},"action_name":"QR_SCENE","expire_seconds":300}                                                                                      
參數信息:{"action_info":{"scene":{"scene_str":"323a31ec-906a-4cb2-a24f-fb846fb16997","scene_id":1}},"action_name":"QR_SCENE","expire_seconds":300}                                                                                      
HTTP POST https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=32_ZTBg5kzh3hUMoaP7xJik8YAFJkoO5gNwjosG1XvI2b4MORGLNitSV4RQSF2JIOx_4MrDGZ5sbNq8bEJ4miwOfK2mcO64TyMIweEUOR5IsY49Kl2vsQuq1plmS2eBQbhzHZVK1HOzFvh125jPPLJhAJALHF
Accept=[application/xml, text/xml, application/json, application/*+xml, application/*+json]                                                                                                                                         
Writing [{"action_info":{"scene":{"scene_id":1,"scene_str":"323a31ec-906a-4cb2-a24f-fb846fb16997"}},"action_name":"QR_SCENE","expire_seconds":300}] with org.springframework.http.converter.StringHttpMessageConverter              
Response 200 OK                                                                                                                                                                                                                     
Reading to [com.alibaba.fastjson.JSONObject]                                                                                                                                                                                        
返回結果:{"ticket":"gQEz8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyU2xwOXhsRGpkLTIxd01nak51MU4AAgQED5NeAwQsAQAA","expire_seconds":300,"url":"http://weixin.qq.com/q/02Slp9xlDjd-21wMgjNu1N"}                                       
                                                                                                                                                                                                                                    

關鍵信息:StringHttpMessageConverter
返回結果:正常.

總結:JSON.toJSON返回Object對象,會調用MappingJackson2XmlHttpMessageConverter,JSON.toJSONString返回String對象,會調用StringHttpMessageConverter,切記雖然轉換的結果都一樣,但是restTemplate調用的轉換器不同,啊啊啊啊啊啊啊.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM