alexg
alexg

Reputation: 922

REST Only - default-interceptor-ref doesn't work

I'm using this as a reference to create a REST only configuration on Struts2:

https://cwiki.apache.org/confluence/display/WW/REST+Plugin

My current problem is with interceptors. I created a sample interceptor that should be executed before the action gets hit.

Here it is:

public class AuthInterceptor extends AbstractInterceptor implements Interceptor
{
    public String intercept(ActionInvocation invocation) throws Exception {

        System.out.println("intercepting AuthInterceptor...");

        return invocation.invoke();
    }

    public void destroy() {
        System.out.println("Destroying AuthInterceptor...");
    }
    public void init() {
        System.out.println("Initializing AuthInterceptor...");
    }
}

and here's my struts.xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="true"/>

    <constant name="struts.mapper.class" value="rest" />

    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="rest-default"/>
    <constant name="struts.convention.package.locators" value="controllers"/>

    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor name="myInterceptor" class="com.company.interceptors.AuthInterceptor"/>
            <interceptor-stack name="myStack">
                <interceptor-ref name="myInterceptor"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="myStack"/>
    </package>
</struts>

The logs (catalina.out) say that my interceptor got initialized, but never actually intercepted anything.

Initializing AuthInterceptor...

Apparently the default-interceptor-ref doesn't work well with the rest mapper class. Is this the case? or am I doing something wrong here?

Upvotes: 2

Views: 834

Answers (2)

The previous respuest is correct but for implement all REST functionality in the interceptor you must change your inteceptor stack to:

<interceptor-stack name="myStack">
    <interceptor-ref name="myInterceptor"/>
    <interceptor-ref name="restDefaultStack"/>
</interceptor-stack>

Upvotes: 1

Roman C
Roman C

Reputation: 1

You need to change this

<constant name="struts.convention.default.parent.package" value="default"/>
<package name="default" extends="rest-default">

The convention plugin when creates action configuration inherits interceptors defined in the parent package. To define a parent package you can use @ParentPackage annotation or the constant above which defines a parent package for all actions.

Upvotes: 2

Related Questions