以下代码为在调用模板推送时获得Access-Token,若缓存存在则缓存中读,缓存过去则重新发起请求
public String getToken() {
if (WxChatCache.AccessToken.expiration <= System.currentTimeMillis()) {
System.out.println("开始请求获取token");
String url = WxChatConstant.Url.ACCESS_TOKEN_URL
.replace("APPID", wxMpProperties.getAppId())
.replace("APPSECRET", wxMpProperties.getSecret());
//http
String s = HttpUtil.get(url);
AccessTokenRespone ac = JSONObject.parseObject(s,AccessTokenRespone.class);
// expiration:为当前执行到此处的时间+2小时有效时间为过期时间
WxChatCache.AccessToken.token = ac.getAccess_token();
WxChatCache.AccessToken.expiration = System.currentTimeMillis()+7200000;
System.out.println("已获取token:"+ac.getAccess_token()+";有效期限为:"+ac.getExpires_in());
return WxChatCache.AccessToken.token;
}
else {
System.out.println("返回缓存中的token:"+ WxChatCache.AccessToken.token);
return WxChatCache.AccessToken.token;
}
}
本地调试时在控制台中能够输出如下结果:
OpenID:xxxxxxxx;=========;content:哈哈哈你好
返回缓存中的token:xxxxxxxxxxxxxxxx
send方法里的token:xxxxxxxxxxxxxxxx
调用的接口地址为:https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=xxxxxxxxxxxxxxxx
然后部署到云托管服务器中时,通过postman发起接口调用,主动推送消息给公众号,提示token为null,无法获取到token,以下是运行日志:
08/18 12:33:18
springboot-plj0-009
OpenID:我的测试用户ID;=========;content:哈哈哈你好呀
08/18 12:33:18
springboot-plj0-009
开始请求获取token========>准备发起http请求获取token
08/18 12:33:18
springboot-plj0-009
APPID:我的公众号ID
08/18 12:33:18
springboot-plj0-009
APPSECRET:我的公众号SECRET
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_275]
08/18 12:33:19
springboot-plj0-009
08/18 12:33:19
springboot-plj0-009
已获取token:null;有效期限为:null=============>此处提示空指针异常,我的请求没办法获取到token
08/18 12:33:19
springboot-plj0-009
send方法里的token:null
08/18 12:33:19
springboot-plj0-009
2022-08-18 04:33:19.607 ERROR 1 --- [p-nio-80-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
08/18 12:33:19
springboot-plj0-009
08/18 12:33:19
springboot-plj0-009
java.lang.NullPointerException: null
08/18 12:33:19
springboot-plj0-009
at java.lang.String.replace(String.java:2240) ~[na:1.8.0_275]
08/18 12:33:19
springboot-plj0-009
at com.tencent.wxcloudrun.service.impl.WxChatServiceImpl.send(WxChatServiceImpl.java:57) ~[classes!/:1.0]
08/18 12:33:19
springboot-plj0-009
at com.tencent.wxcloudrun.service.impl.WxChatServiceImpl.sendTest(WxChatServiceImpl.java:43) ~[classes!/:1.0]
08/18 12:33:19
springboot-plj0-009
at com.tencent.wxcloudrun.controller.GirlFriendController.sendTest(GirlFriendController.java:54) ~[classes!/:1.0]
08/18 12:33:19
springboot-plj0-009
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_275]
08/18 12:33:19
springboot-plj0-009
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_275]
08/18 12:33:19
springboot-plj0-009
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_275]
08/18 12:33:19
springboot-plj0-009
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_275]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar!/:5.3.10]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.53.jar!/:na]
08/18 12:33:19
springboot-plj0-009
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:769) [tomcat-embed-core-9.0.53.jar!/:na]
有没有大哥能指导一下小弟呀,感激不尽
