开发问题:postman调试正常,在项目使用restTemplate发送就是不成功.

时间:2020-04-12
本文章向大家介绍开发问题:postman调试正常,在项目使用restTemplate发送就是不成功.,主要包括开发问题: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调用的转换器不同,啊啊啊啊啊啊啊.

原文地址:https://www.cnblogs.com/rickq95/p/12687602.html