简体   繁体   English

Tomcat 异常 提交响应后无法调用 sendError()?

[英]Tomcat exception Cannot call sendError() after the response has been committed?

While doing some operations in my application I got在我的应用程序中进行一些操作时,我得到了

java.lang.IllegalStateException Cannot call sendError() java.lang.IllegalStateException 无法调用 sendError()

When I reload the page again it work some time properly, but after some time again it shows the same exception.当我再次重新加载页面时,它会正常工作一段时间,但一段时间后它会显示相同的异常。 How can I overcome this exception?我怎样才能克服这个例外?

Below is the exception:以下是例外情况:

HTTP Status 500 - Cannot call sendError() after the response has been committed
type Exception report
message Cannot call sendError() after the response has been committed
description The server encountered an internal error that prevented it from fulfilling this request.
exception 
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:451)
org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:725)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.40 logs.

Struts.xml Struts.xml

<struts>
    <package name="default" extends="hibernate-default">
        <action name="addUser" method="add" class="com.demo.action.UserAction">
            <result name="input">/register.jsp</result>
            <result name="success" type="redirect">list</result>
        </action>
        <action name="list" method="list" class="com.demo.action.UserAction">
            <interceptor-ref name="basicStackHibernate" />
            <result name="success">/list.jsp</result>
        </action>
    </package>
</struts>

This error is a symptom of some other problem, not the root cause you're looking for.此错误是其他问题的症状,而不是您要查找的根本原因。

This error explains why the user can't be redirected to the error page.这个错误解释了为什么用户不能被重定向到错误页面。 (Reason: the server has already flushed part of the response buffer back to the client - it's too late to switch/redirect to the error page.) (原因:服务器已经将响应缓冲区的一部分刷新回客户端 - 切换/重定向到错误页面为时已晚。)

As the error message points out, check elsewhere in your Apache Tomcat 7 logs (or debug your app another way) to find what is throwing an exception.正如错误消息指出的那样,检查 Apache Tomcat 7 日志中的其他地方(或以其他方式调试您的应用程序)以查找引发异常的原因。

I was creating a @ManyToOne and @OneToMany relationship.我正在创建 @ManyToOne 和 @OneToMany 关系。 I added @JsonIgnore above the @ManyToOne and it solved the error.我在@JsonIgnore上方添加了 @JsonIgnore 并解决了错误。

我通过将@jsonIgnore添加到另一个对象列表的所有 getter 来解决此错误

For others in my situation--What was happening was that I had two @Entity objects with a many to many relationship causing infinite json to be generated, causing spring security to throw this error.对于我这种情况的其他人 - 发生的事情是我有两个具有多对多关系的 @Entity 对象,导致生成无限的 json,从而导致 Spring Security 抛出此错误。 Try adding @JsonIgnore above your hibernate relationships.尝试在您的休眠关系上方添加 @JsonIgnore。

Note: there are 2 @JsonIgnore dependencies you can import.注意:您可以导入 2 个@JsonIgnore依赖项。 Make sure it's from Jackson library;确保它来自杰克逊图书馆; that made the difference for me:这对我来说很重要:

import com.fasterxml.jackson.annotation.JsonIgnore;

This is a common error and there can be various root cause can be identified.这是一个常见错误,可以确定各种根本原因。 In my case I were opening pdf file from the web service and for this I were performing write operation in file using buffer.就我而言,我从 Web 服务打开 pdf 文件,为此我使用缓冲区在文件中执行写操作。 So kindly change below:所以请在下面更改:

        File outfile = File.createTempFile("temp", ".pdf");

        OutputStream os=new FileOutputStream(outfile);
        byte[] buffer = new byte[1024];

        int length;
        /*copying the contents from input stream to
         * output stream using read and write methods
         */
        while ((length = is.read(buffer)) > 0){
            os.write(buffer, 0, length);
        }

to

        File outfile = File.createTempFile("temp", ".pdf");
        IOUtils.copy(is, new FileOutputStream(outfile));

and after this I were performing below operation:在此之后,我正在执行以下操作:

    javax.ws.rs.core.Response.ResponseBuilder responseBuilder = javax.ws.rs.core.Response
                .ok(outfile, MediaType.APPLICATION_OCTET_STREAM);
                responseBuilder.header("content-type","application/pdf");

             return responseBuilder.build();

and error get resolved.和错误得到解决。 Cheers!干杯!

You can try this Annotation it will help as it fixed my issue.你可以试试这个 Annotation 它会帮助解决我的问题。

It will definitely help if it will not be able to help you, then try to modify this as per your requirement.如果它无法帮助您,它肯定会有所帮助,然后尝试根据您的要求对其进行修改。

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
   @JsonIdentityReference(alwaysAsId=true)

This is what caused it in my case.这就是在我的情况下导致它的原因。

I have 2 filters that both have the capability to send an error through the HttpServletResponse.sendError() method.我有 2 个过滤器,它们都能够通过HttpServletResponse.sendError()方法发送错误。 If Filter A discovered something wrong and called sendError on the HttpServletResponse object, then a second call in the same filter or in filter B will cause the cannot call senderror exception.如果过滤器 A 发现错误并在 HttpServletResponse 对象上调用了 sendError,那么在同一个过滤器或过滤器 B 中的第二次调用将导致cannot call senderror异常。 This is because sendError does not cause the request itself to be aborted.这是因为 sendError 不会导致请求本身被中止。 The code in the filter continues to be executed after the sendError method had been called.在调用 sendError 方法后,过滤器中的代码将继续执行。

在@ManyToOne 上方使用@JsonIgnore

if u use annotations like:@WebFilter,@WebServlet...,just add metadata-complete="true" into web.xml eg如果您使用注释,如:@WebFilter,@WebServlet...,只需将 metadata-complete="true" 添加到 web.xml 中,例如

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         id="WebApp_ID" metadata-complete="true" version="3.0">

Hope this is your need!希望这是您的需要!

There are so many answers to use @JsonIgnore annotation.使用@JsonIgnore注释的答案@JsonIgnore But I will not recommend it.但我不会推荐它。 If your parent class has a many-to-one relationship with single entity then it is fine.如果您的父类与单个实体是many-to-one关系,那就没问题了。 But if your parent class has two many-to-one relationships then definitely it gives a headache to you.但是如果你的父类有两个多对一的关系,那么肯定会让你头疼。

I will suggest going with a uni-directional approach and implement a separate dto class based on your requirement.我会建议采用单向方法并根据您的要求实现一个单独的dto类。

I was using Jersey and I returned the following response我正在使用 Jersey 并返回以下响应

Response.status(HttpStatus.SC_MOVED_TEMPORARILY).header("Location", "https://example.com?param1=foo bar").build()

After URL encoding, the issue was solved URL编码后,问题解决

Response.status(HttpStatus.SC_MOVED_TEMPORARILY).header("Location", "https://example.com?param1=foo%20bar").build()

Adding @JsonIgnore solved the current error.添加@JsonIgnore 解决了当前的错误。 However, it cause error when creating entry of entity with @ManyToOne annotation.但是,在创建带有@ManyToOne 注释的实体条目时会导致错误。

Using @JsonBackReference(value = "**") (where ** is the property of the entity with @OneToMany annotation) can also solve this error while not causing other errors!使用@JsonBackReference(value = "**")(其中**是带有@OneToMany注解的实体的属性)也可以解决这个错误,同时不会引起其他错误!

(in my case, Im using User object with @ManyToOne annotation, but Im using the id of User as a column in sql table) (在我的例子中,我使用带有@ManyToOne 注释的用户 object,但我使用用户的 ID 作为 sql 表中的列)

@ManyToOne 映射上的 @JsonIgnore 将解决它。

I have overcome the exception,you may check your table structure.I execute the sql query produced by hibernate in mysql.我已经克服了这个异常,你可以检查你的表结构。我在mysql中执行hibernate产生的sql查询。 As excepted,an exception was thrown.作为exception,抛出异常。

[Err] 1054 - Unknown column 'this_.ID' in 'field list'

I found that a field ID is missing in the table.Add the ID into the table ,it's ok.我发现表中缺少一个字段ID。将ID添加到表中,就可以了。 Hope it's helpful.希望它有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 响应提交后无法调用 sendError() - Cannot call sendError() after the response has been committed Spring boot:java.lang.IllegalStateException:响应提交后无法调用sendError() - Spring boot : java.lang.IllegalStateException: Cannot call sendError() after the response has been committed Hibernate和Jackson(java.lang.IllegalStateException:提交响应后无法调用sendError()) - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) 使用spring和hibernate来获取多个表数据无法正常工作-UI-提交响应后无法调用sendError() - Fetching multiple table data using spring and hibernate not working as excepted in UI-Cannot call sendError() after the response has been committed 如何解决:提交响应后无法调用 sendRedirect() - How to fix: Cannot call sendRedirect() after the response has been committed 在Struts2中提交响应后无法调用sendRedirect() - Cannot call sendRedirect() after the response has been committed in Struts2 HTTP状态500-提交响应后无法转发TOMCAT 7.55 - HTTP Status 500 - Cannot forward after response has been committed TOMCAT 7.55 将tomcat 7.0.59升级到7.0.61:提交响应后无法创建会话 - Upgrade tomcat 7.0.59 to 7.0.61 : Cannot create a session after the response has been committed 插入数据后提交响应后无法转发 - After inserting data Cannot forward after response has been committed java.lang.IllegalStateException:在提交响应后无法调用sendRedirect() - java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM