0
votes

i am trying to create a restful service using struts2. when i added the struts2-rest-plugin in the dependency struts action is not taking the default method(execute).It is looking for create() or index() methods .

struts2-convention-plugin and struts2-rest-plugin

<dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>${struts.version}</version>
</dependency>
<dependency>
             <groupId>org.apache.struts</groupId>
             <artifactId>struts2-rest-plugin</artifactId>
             <version>${struts.version}</version>
</dependency>

I have all these constants in struts.xml

    <constant name="struts.devMode" value="false" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.action.extension" value="action," />
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources,errors,global" />
    <constant name="struts.multipart.maxSize" value="2097152" />
    <constant name="struts.ui.theme" value="css_xhtml" />
    <constant name="struts.codebehind.pathPrefix" value="/WEB-INF/pages/" />
    <constant name="struts.enable.SlashesInActionNames" value="true" />
    <constant name="struts.convention.action.disableScanning" value="true" />
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
   <constant name="struts.convention.package.locators" value="controller"/> 
   <constant name="struts.convention.action.suffix" value="Controller"/> 
   <constant name="struts.convention.action.mapAllMatches" value="false"/>
   <constant name="struts.convention.default.parent.package" value="rest-default"/>

I am getting the error when i execute the web project

ERROR [tomcat-http--34] Dispatcher.error(38) | Exception occurred during processing request: com.vxl.appanalytix.webapp.action.LoginAction.create()
java.lang.NoSuchMethodException: com.vxl.appanalytix.webapp.action.LoginAction.create()
    at java.lang.Class.getMethod(Class.java:1655)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.getActionMethod(AnnotationValidationInterceptor.java:75)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:47)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:138)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:138)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:238)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:138)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:238)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

my project has spring for dependency management, insted of using the spring MCV we planned for using struts2.I tried to use spring restful service but I was getting some dispacher error so i moved to struts2-rest-plugin. here the controller class i implemented as a service has methods like index() and create() for HTTP GET and PUT respectivelly. how exactly the rest plugin works.

2
action mapping '<action name="signIn" class="com.vxl.appanalytix.webapp.action.LoginAction" method="execute"><result name="success" type="redirectAction">mClients</result> </action>'jos
Is the value for constant name struts.action.extension correct.? Why is there a comma..? Is it a typo.?user2339071
This constant allows actions with the suffixes of ", ".action"jos

2 Answers

1
votes

Using the struts2-rest plugin, along with the convention-plugin means that your action class doesn't have the execute method as the default anymore. In fact, you may not have an execute method and still have a valid REST-ful controller.

The way how the URL gets mapped to your classes is defined by the convention-plugin. This is clearly explained here: http://struts.apache.org/docs/convention-plugin.html

Further, the struts.rest.namespace-approach didn't work for me. Somehow, Struts is failing to work that way. Instead, I did the following:-

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

This way I tricked Struts into feeling that the namespace for my REST Web service calls is /rest.