各位老师们,这个问题困扰我多时了,还请不吝赐教,感激不尽!
问题描述:之前一直用jfinal3.2,并且用可jfinal-weixin做微信公众号开发,一直都正常,最近上了新项目,要求换成jfinal4.5,就出现各种问题,其中一个问题就是创建微信菜单时报错,具体情况如下:
先介绍代码的情况:
代码框架结构为jfinal4.5+jfinal-weixin2.1,主要代码为:
public void createMenu() { ApiResult apiResult = MenuApi.createMenu(getMenuJsonStr()); if (apiResult.isSucceed()){ System.out.print("创建成功:--"+apiResult.getJson()); renderText(apiResult.getJson()); } else { System.out.print("创建失败:--" + apiResult.getJson()); renderText(apiResult.getErrorMsg()); } }
核心功能就是调用jfinal-weixin中的MenuApi.createMenu方法,创建微信菜单,其中传入的json字符串是没问题的,问题现象为:代码编译重启服务,调用createMenu创建菜单,第一次运行报错信息为:
[ERROR] AppInterceptor: ---Exception Log Begin--- 控制器:com.weixin.controller.WxApiController 方法:createMenu 异常类型:java.lang.RuntimeException 异常明细: java.lang.RuntimeException: java.lang.NoSuchMethodError: com.alibaba.fastjson.serializer.SerializeConfig.put(Ljava/lang/reflect/Type;Lcom/alibaba/fastjson/serializer/ObjectSerializer;)Z at com.jfinal.aop.Invocation.invoke(Invocation.java:107) at com.jfinal.weixin.sdk.jfinal.ApiInterceptor.intercept(ApiInterceptor.java:28) at com.jfinal.aop.Invocation.invoke(Invocation.java:91) at com.config.interceptor.AppInterceptor.intercept(AppInterceptor.java:22) at com.jfinal.aop.Invocation.invoke(Invocation.java:91) at com.jfinal.core.ActionHandler.handle(ActionHandler.java:82) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:89) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NoSuchMethodError: com.alibaba.fastjson.serializer.SerializeConfig.put(Ljava/lang/reflect/Type;Lcom/alibaba/fastjson/serializer/ObjectSerializer;)Z at com.jfinal.json.FastJson.<clinit>(FastJson.java:31) at com.jfinal.weixin.sdk.utils.JsonUtils.parse(JsonUtils.java:111) at com.jfinal.weixin.sdk.api.AccessToken.<init>(AccessToken.java:34) at com.jfinal.weixin.sdk.api.AccessTokenApi$1.call(AccessTokenApi.java:102) at com.jfinal.weixin.sdk.api.AccessTokenApi$1.call(AccessTokenApi.java:97) at com.jfinal.weixin.sdk.utils.RetryUtils.retryOnException(RetryUtils.java:32) at com.jfinal.weixin.sdk.api.AccessTokenApi.refreshAccessToken(AccessTokenApi.java:97) at com.jfinal.weixin.sdk.api.AccessTokenApi.getAccessToken(AccessTokenApi.java:57) at com.jfinal.weixin.sdk.api.AccessTokenApi.getAccessTokenStr(AccessTokenApi.java:83) at com.jfinal.weixin.sdk.api.MenuApi.createMenu(MenuApi.java:38) at com.weixin.controller.WxApiController.createMenu(WxApiController.java:109) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.jfinal.aop.Invocation.invoke(Invocation.java:97) ... 22 more 无效的请求或请求异常!
其中,com.alibaba.fastjson的版本是1.2.7
不重启服务,再次运行报的错又不一样了,并且之后报的错都是如下信息:
[ERROR] AppInterceptor: ---Exception Log Begin--- 控制器:com.weixin.controller.WxApiController 方法:createMenu 异常类型:java.lang.RuntimeException 异常明细: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class com.jfinal.json.FastJson at com.jfinal.aop.Invocation.invoke(Invocation.java:107) at com.jfinal.weixin.sdk.jfinal.ApiInterceptor.intercept(ApiInterceptor.java:28) at com.jfinal.aop.Invocation.invoke(Invocation.java:91) at com.config.interceptor.AppInterceptor.intercept(AppInterceptor.java:22) at com.jfinal.aop.Invocation.invoke(Invocation.java:91) at com.jfinal.core.ActionHandler.handle(ActionHandler.java:82) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:89) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.jfinal.json.FastJson at com.jfinal.weixin.sdk.utils.JsonUtils.parse(JsonUtils.java:111) at com.jfinal.weixin.sdk.api.AccessToken.<init>(AccessToken.java:34) at com.jfinal.weixin.sdk.api.AccessTokenApi$1.call(AccessTokenApi.java:102) at com.jfinal.weixin.sdk.api.AccessTokenApi$1.call(AccessTokenApi.java:97) at com.jfinal.weixin.sdk.utils.RetryUtils.retryOnException(RetryUtils.java:32) at com.jfinal.weixin.sdk.api.AccessTokenApi.refreshAccessToken(AccessTokenApi.java:97) at com.jfinal.weixin.sdk.api.AccessTokenApi.getAccessToken(AccessTokenApi.java:57) at com.jfinal.weixin.sdk.api.AccessTokenApi.getAccessTokenStr(AccessTokenApi.java:83) at com.jfinal.weixin.sdk.api.MenuApi.createMenu(MenuApi.java:38) at com.weixin.controller.WxApiController.createMenu(WxApiController.java:109) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.jfinal.aop.Invocation.invoke(Invocation.java:97) ... 22 more 无效的请求或请求异常!
此时,错误类型变了,报错的包也变成com.jfinal.json.FastJson
看到这些错误也在网上查阅了一些资料,也检查过jar包并无冲突,经过多次尝试,都未能解决这个问题,还望各位老师和大佬们指点迷津,感激不尽!
用到 jfinal weixin 项目的 MsgController 时,需要在 configRoutes 中配置 me.setMappingSuperClass(true)
升级一定要先看文档,内容极少, 5 分钟看完足够了:
https://www.jfinal.com/doc/14-1