关于fastjson的setSafeMode错误

<!--JFinal 相关-->
<dependency>
  <groupId>com.jfinal</groupId>
  <artifactId>jfinal-undertow</artifactId>
  <version>2.2</version>
</dependency>
<dependency>
  <groupId>com.jfinal</groupId>
  <artifactId>jfinal</artifactId>
  <version>4.9.03</version>
</dependency>
<!--JFinal 相关-->
<!--JSON-->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.74</version>
</dependency>
<!--JSON-->

有时编译执行报错,有时是部署后才报错

java.lang.RuntimeException: java.lang.NoSuchMethodError: com.alibaba.fastjson.parser.ParserConfig.setSafeMode(Z)V

java.lang.RuntimeException: java.lang.NoSuchMethodError: com.alibaba.fastjson.parser.ParserConfig.setSafeMode(Z)V
	at com.jfinal.aop.Invocation.invoke(Invocation.java:107)
	at com.jfinal.core.ActionHandler.handle(ActionHandler.java:88)
	at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:90)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
	at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
	at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
	at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
	at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
	at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
	at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
	at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
	at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
	at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
	at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.SessionRestoringHandler.handleRequest(SessionRestoringHandler.java:119)
	at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130)
	at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78)
	at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99)
	at io.undertow.server.Connectors.executeRootHandler(Connectors.java:376)
	at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodError: com.alibaba.fastjson.parser.ParserConfig.setSafeMode(Z)V
	at com.jfinal.json.FastJson.<clinit>(FastJson.java:38)
	at com.jfinal.json.FastJsonFactory.getJson(FastJsonFactory.java:34)
	at com.jfinal.json.Json.getJson(Json.java:63)
	at com.jfinal.kit.JsonKit.toJson(JsonKit.java:28)
	at com.jfinal.plugin.activerecord.Model.toJson(Model.java:1032)
	at com.xxkd.index.IndexController.k3Api(IndexController.java:202)
	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)
	... 34 more



public class ResultCode extends Model<ResultCode>
ResultCode resultCode = new ResultCode();
resultCode.set("code", 400).set("msg", "error").set("data", strUrl);
renderJson(resultCode.toJson());  <--此处报错


评论区

JFinal

2020-11-23 10:44

你用的 fastjson 是 1.2.74 版本,这个版本是没有问题的,估计是你出问题的时候使用的 "其实" 是老版本的 fastjson

老版本的 fastjson 没有 setSafeMode(...) 方法

由于 fastjson 出现过几次重大 bug,所以后来添加了 setSafeMode(...),jfinal 中默认使用的它的 safeMode

farce

2020-11-23 10:58

确实是老版本,maven之前没有进行reload,lib中还是老版本

热门反馈

扫码入社