SpringMVC错误记录

本文探讨了在Spring MVC环境下Hibernate抛出的无法为当前线程获取同步session异常,并提供了具体的解决方案。通过调整spring-mvc.xml配置文件中的组件扫描设置来避免重复扫描@Service注解,从而解决了问题。
这里写图片描述 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中使用的是这三个依赖包:
这里写图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值