Could not obtain transaction-synchronized Session for current thread
在junit下测试是没有问题的,但是在springmvc下就会抛出异常:无法为当前线程获取同步session
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="pers.msidolphin.sshdemo.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 整合hibernate相关配置 -->
<import resource="classpath:pers/msidolphin/sshdemo/resources/spring-config/applicationContext-hibernate.xml"/>
<!-- 可能用到的bean配置 -->
<import resource="classpath:pers/msidolphin/sshdemo/resources/spring-config/applicationContext-bean.xml"/>
</beans>
spring-mvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="pers.msidolphin.sshdemo.*">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean name="/users.action" class="pers.msidolphin.sshdemo.controller.TestController"></bean>
<!-- 配置映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 配置适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
<!-- 注解形式 -->
<!--
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
-->
<!-- 这一条可代替上面两条 -->
<mvc:annotation-driven/>
</beans>
applicationContext.xml是有事务管理的,但是spring-mvc.xml是没有配置的,所以解决方法是在spring-mvc.xml配置文件中不要再次扫描@Service注解
<context:component-scan base-package="pers.msidolphin.sshdemo.*">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
Java.lang.IllegalArgumentException: No converter found for return value of type: xxxx
导入的jar包版本错误
前端无法接收后端返回的json数据(请求的格式json),返回415状态码:
415状态码表示:请求的提交的格式不是服务器所支持的类型。这应该是解析json时出错
当请求的格式为key/value时,返回500状态码并抛出以上异常:
这大概就是Spring MVC所依赖的json jar包导错了
当时使用的spring版本为4.3.8,而引入的jackson依赖包为:
但是在spring4中使用的是这三个依赖包:
本文探讨了在Spring MVC环境下Hibernate抛出的无法为当前线程获取同步session异常,并提供了具体的解决方案。通过调整spring-mvc.xml配置文件中的组件扫描设置来避免重复扫描@Service注解,从而解决了问题。
669

被折叠的 条评论
为什么被折叠?



