Json plugin and global exception in Struts

I am trying to return a global exception in JSON format. This is my current struts.xml. I'm not sure what I am missing.

<struts>

<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="global" />
<constant name="struts.configuration.xml.reload" value="true" />

<package name="mkaStrive" extends="json-default">
    <interceptors>
        <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />
        <interceptor-stack name="mobileStack">
            <interceptor-ref name="json" />
            <interceptor-ref name="defaultStack" />
        </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="test" />

    <global-results>

        <!-- Exceptions are handled by ExceptionAction -->
        <result name="exception" type="chain">
            <param name="actionName">exception</param>
        </result>

    </global-results>

    <global-exception-mappings>
        <exception-mapping exception="java.lang.Throwable" result="exception" />
    </global-exception-mappings>

    <action name="exception" class="n.a.exception.ExceptionAction" />

    <action name="getQuestionsList" class="n.a.mkastrive.action.GetQuestions" method="execute">
        <interceptor-ref name="json" />
        <result type="json"></result>
    </action>       
</package>

The GetQuestions action for the moment simply throws an exception:

public String execute() throws Exception {
    throw new Exception("TEST");
}

From here it is perfectly clear that I have global results, and then a chain to action called exception.

+4
source share
1 answer

The only problem I see in struts.xmlthat you provide is the following: (typo?)

Using

<default-interceptor-ref name="mobileStack" />

Instead

<default-interceptor-ref name="test" />

At the same time, you can verify that the control is included in the method of executethe class exception action: n.a.exception.ExceptionAction.

, , , json :

<action name="exception" class="n.a.exception.ExceptionAction" >
  <result type="json"></result>
</action>

Exception, GetQuestions.execute(), execute:

(Exception)ActionContext.getContext().getValueStack().findValue("exception");

, getters json-. json.

+1

Source: https://habr.com/ru/post/1695728/


All Articles