web-dev-qa-db-ja.com

Tomcat例外応答がコミットされた後、sendError()を呼び出すことはできませんか?

私のアプリケーションでいくつかの操作をしている間、私は

Java.lang.IllegalStateException sendError()を呼び出すことができません

ページを再度リロードすると、適切に動作しますが、しばらくすると同じ例外が表示されます。どうすればこの例外を克服できますか?

以下は例外です。

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>
    <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>
23
user3117840

このエラーは、探している根本的な原因ではなく、他の問題の症状です。

このエラーは、ユーザーをエラーページにリダイレクトできない理由を説明しています。 (理由:サーバーは既に応答バッファーの一部をクライアントにフラッシュして戻しています-エラーページに切り替える/リダイレクトするには遅すぎます。)

エラーメッセージが指摘しているように、Apache Tomcat 7ログの別の場所を確認(または別の方法でアプリをデバッグ)して、例外をスローしているものを見つけます。

25
Barett

別のオブジェクトのリストのすべてのゲッターに@jsonIgnoreを追加して、このエラーを解決しました

6
Medo

私は@ManyToOneと@OneToManyの関係を作成していました。 @ManyToOneの上に@JsonIgnoreを追加し、エラーを解決しました。

4
Asad

これは一般的なエラーであり、さまざまな根本原因を特定できます。私の場合、Webサービスからpdfファイルを開いていたため、バッファーを使用してファイルに書き込み操作を実行していました。以下のように親切に変更してください:

        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);
        }

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

そしてこの後、私は以下の操作を実行していました:

    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();

エラーが解決されます。乾杯!

1
Aman Goel

私の状況の他の人のために-何が起こっていたのですか?2つの@Entityオブジェクトが多対多の関係であり、無限のjsonが生成され、春のセキュリティがこのエラーをスローしました。休止状態の関係の上に@JsonIgnoreを追加してみてください。

1
FlexEast

@ 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">

これがあなたの必要であることを願っています!

0
Derry

私の問題を解決するのに役立つこの注釈を試すことができます。

それがあなたを助けることができないならば、それは間違いなく助けます、そして、あなたの要件に従ってこれを修正しようとします。

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