微信API对接SDK

1、微信开发者模式及API对接配置

2、接收和回复消息

(1)、编写消息接收Servlet,并配置到web.xml

  1. 通过继承 com.dodo.weixin.servlet.WinxinServlet 快速完成
  2. 实现2个方法:
  3. // 获取微信自定义账户,配置文件中自定义的账户名称
  4. public abstract String getWeChatAccount();
  5. //添加消息接收监听器
  6. //通过实现RequestMsgListener来自定义消息接收监听器
  7. //通过继承RequestMsgAdapter也可以
  8. public abstract List<RequestMsgListener> getRequestMsgListeners();

消息接收监听器,可处理以下消息

  1. public class RequestMsgAdapter implements RequestMsgListener {
  2. private static final Logger LOGGER = LoggerFactory.getLogger(RequestMsgAdapter.class);
  3. //接收到文本消息
  4. public void onTextMsg(RequestMsgText msg, HttpServletRequest request) {
  5. LOGGER.debug("Received:", msg.toString());
  6. }
  7. //接收到图片消息
  8. public void onImageMsg(RequestMsgImage msg, HttpServletRequest request) {
  9. LOGGER.debug("Received:", msg.toString());
  10. }
  11. //收到事件推送消息
  12. public void onEventMsg(RequestMsgEvent msg, HttpServletRequest request) {
  13. LOGGER.debug("Received:", msg.toString());
  14. }
  15. //接收到链接消息
  16. public void onLinkMsg(RequestMsgLink msg, HttpServletRequest request) {
  17. LOGGER.debug("Received:", msg.toString());
  18. }
  19. //接收到LBS消息
  20. public void onLocationMsg(RequestMsgLocation msg, HttpServletRequest request) {
  21. LOGGER.debug("Received:", msg.toString());
  22. }
  23. //语音识别消息
  24. public void onVoiceMsg(RequestMsgVoice msg, HttpServletRequest request) {
  25. LOGGER.debug("Received:", msg.toString());
  26. }
  27. //接收到视频消息
  28. public void onVideoMsg(RequestMsgVideo msg, HttpServletRequest request) {
  29. LOGGER.debug("Received:", msg.toString());
  30. }
  31. //接收到错误消息
  32. public void onErrorMsg(RequestMsgError msg, HttpServletRequest request) {
  33. LOGGER.debug("Received:", msg.toString());
  34. }
  35. }

(2)、将该Servlet的访问地址,配置在微信开发者模式下即完成对接

3、其他接口对接,开箱即用

  1. //二维码相关接口
  2. com.dodo.weixin.utils.WechatQrCodeUtil
  3. //客服消息接口
  4. com.dodo.weixin.utils.WeixinCustomeMsgUtil
  5. //媒体接口
  6. com.dodo.weixin.utils.WeixinMediaUtil
  7. //自定义菜单接口
  8. com.dodo.weixin.utils.WeixinMenuUtil
  9. //多客服接口
  10. com.dodo.weixin.utils.WeixinMultiCustomerUtil
  11. //模板消息接口
  12. com.dodo.weixin.utils.WeixinTemplateMsgUtil
  13. //URL转换接口
  14. com.dodo.weixin.utils.WeixinUrlUtil
  15. //用户分组接口
  16. com.dodo.weixin.utils.WeixinUserGroupUtil
  17. //用户信息接口
  18. com.dodo.weixin.utils.WeixinUserInfoUtil
  19. //网页授权接口
  20. com.dodo.weixin.utils.WeixinWebAuthUtil