【问题标题】:Cannot call senderror() after response has been committed Tomcat Spring MVC提交响应后无法调用 senderror() Tomcat Spring MVC
【发布时间】:2017-01-30 02:19:43
【问题描述】:

如果我在我的控制器类中返回一个 Employee 对象,我会从我的 REST 服务中得到响应。但是如果我试图返回一个列表,那么我经常会收到这个错误:

错误:提交响应后无法调用 senderror() (还附上图片) 我正在使用 Eclipse、tomcat、maven 并制作 Spring REST 服务。

这是我的控制器类代码 sn-p,它正在返回 Employee,一切都很好,我可以在浏览器上看到我的 JSON 数据。但是当我将 Employee 更改为 List 作为我的新返回类型时,我收到以下错误并且我的 Eclipse 也挂起几分钟。我认为我的问题可能与 JSON 转换器有关。请帮忙。

@Controller
@RequestMapping("/emp")
public class EMPController {

@Autowired
EmployeeService empservice;
@RequestMapping("byname3/{name}")
@ResponseBody
public Employee getByName(@PathVariable String name) {
    System.out.println("Inside getByName()..successfully..EMPController");
    .....
    ......

我的 dispatcher-servlet.xml

<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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <context:component-scan base-package="com.spice.controller" />
    <context:component-scan base-package="com.spice.daoImpl" />
    <context:component-scan base-package="com.spice.serviceImpl " />

    <mvc:annotation-driven />   <!-- tHIS HELPS IN AUTOMATIC JSON-TO-JAVA AND VICE VERSA CONVERTUION -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
        <property name="username" value="tiwari"></property>
        <property name="password" value="tiwari"></property>
    </bean>

    <bean id="mysessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- eARLIER i WAS USING org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean -->
        <!-- BUT THIS LEADS TO SOME CACHE PROVIDER CLASS NOT FOUND EXCEPTION -->
        <!-- ALSO THIS AnnotationSessionFactoryBean IS NOW REPLACED BY LocalSessionFactoryBean -->
        <property name="dataSource" ref="dataSource"></property>

         <property name="annotatedClasses">
            <list>
                <value>com.spice.beans.Employee</value>
                <value>com.spice.beans.Address</value>
            </list>
        </property>


        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <!-- THIS CLASS IS COMPATIBLE WITH ABOVE VERSIONS OF SPRING AND HIBERNATE  -->  
        <property name="sessionFactory" ref="mysessionFactory" />  
    </bean>

</beans>

【问题讨论】:

    标签: spring rest


    【解决方案1】:

    错误:提交响应后无法调用 senderror() (另附图片)

    此错误是另一个错误的后果。服务器遇到您的应用程序的主要异常,因此它尝试调用 HttpServletResponse#sendError(),这又会引发 IllegalStateException。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2018-12-06
      相关资源
      最近更新 更多