spring bean
<!-- 微信配置 --> <bean id="wxMpConfigStorage" class="me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage"> <property name="appId" value="#{config['mp.appId']}"></property> <property name="secret" value="#{config['mp.secret']}"></property> <property name="token" value="#{config['mp.token']}"></property> </bean>
java 服务类
@Autowired private WxMpConfigStorage wxMpConfigStorage;
private WxMpService wxMpService; /** * 获取第三方微信服务类 * */ public WxMpService getWxMpService(){ if(wxMpService == null){ WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage); this.wxMpService = wxMpService; return wxMpService; }else{ return wxMpService; } }
测试类
@Autowired private WeixinService weixinService; @Test public void createCode() throws WxErrorException{ WxMpService wxMpService = weixinService.getWxMpService(); WxMpQrCodeTicket ticket = wxMpService.qrCodeCreateLastTicket("abc"); String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket; System.out.println(url); }
注意:
未授权时调用报错:
{"errcode":48001,"errmsg":"api unauthorized hint: [KQ2xDA0318vr22]"}