背景:
SpringBoot 项目中,Mybatis开启了二级缓存 + 开启了懒加载,调用接口时,第一次能正常返回数据,第二次及以后每次都返回错误,部分日志:
[ ERROR] [2021-04-14 14:02:33.166] com.xxx.handle.ExceptionHandle [42] [http-nio-127.0.0.1-9980-exec-2] [handleException] - Could not write content: Cannot get Configuration as configuration factory was not set. (through reference chain: com.xxx.ssc.utils.ResultEntity["data"]->com.xxx.util.TableResultView["rows"]->java.util.ArrayList[0]->com.xxx.api.storage.model.UserModel_$$_jvst753_0["role"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Cannot get Configuration as configuration factory was not set. (through reference chain: com.xxx.ssc.utils.ResultEntity["data"]->com.xxx.util.TableResultView["rows"]->java.util.ArrayList[0]->com.xxx.api.storage.model.UserModel_$$_jvst753_0["role"])
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Cannot get Configuration as configuration factory was not set. (through reference chain: com.xxx.ssc.utils.ResultEntity["data"]->com.xxx.util.TableResultView["rows"]->java.util.ArrayList[0]->com.xxx.api.storage.model.UserModel_$$_jvst753_0["role"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Cannot get Configuration as configuration factory was not set. (through reference chain: com.smartxp.authlib.ssc.utils.ResultEntity["data"]->com.xxx.util.TableResultView["rows"]->java.util.ArrayList[0]->com.xxx.api.storage.model.UserModel_$$_jvst753_0["role"])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:274)
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:232)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:173)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:130)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
errorMessage;
"Could not write content: Cannot get Configuration as configuration factory was not set. (through reference chain: com.xxx.ssc.utils.ResultEntity[\"data\"]->com.xxx.util.TableResultView[\"rows\"]->java.util.ArrayList[0]->com.xxx.api.storage.model.UserModel_$$_jvst753_0[\"role\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Cannot get Configuration as configuration factory was not set. (through reference chain: com.smartxp.authlib.ssc.utils.ResultEntity[\"data\"]->com.xxx.util.TableResultView[\"rows\"]->java.util.ArrayList[0]->com.xxx.api.storage.model.UserModel_$$_jvst753_0[\"role\"])",
从日志可以看出,和缓存有关。
解决方法:
方法1、(全局)关闭懒加载
<setting name="lazyLoadingEnabled" value="false" />
方法2:关闭缓存
<select id="selectStorageUserByExample" resultMap="ResultStorageWithRole" useCache="false">
方法3:在集合关联对象collection标签中,禁用懒加载
<collection property="role" fetchType="eager" ofType="java.lang.Long" select="findRoleId" column="{uId=id}">
</collection>
fetchType="eager"