reststrutsstruts2-convention-plugin

struts2-rest plugin..making both struts actions + rest actions work together but. giving java.lang.reflect.InvocationTargetException


I am converting my existing struts 2 application to serve through some rest based services also.

I have used two plugins, struts2-rest plugin and struts-convention plugin,

Along with these I have also used asm.jar because above was giving a class not found exception which was there in asm jar.

I want to have both the functionalities as ..my normal struts action mappings should also work along with rest urls.

struts.xml

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

<struts>
  <!--  Overwrite Convention -->

<constant name="struts.convention.action.mapAllMatches" value="true"/>    
<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.default.parent.package" value="rest-default"/>
<constant                name="struts.mapper.class"value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
<constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts" />

<package name="userlogin" extends="struts-default" namespace="/">
    <action>
                    ......
    </action>
</package>

<package name="secureAction" extends="struts-default,json-default"
    namespace="/secure">
    <interceptors>

        ...............

    </interceptors>

    <default-interceptor-ref name="secureStack" />
    <global-results>
        .............
    </global-results>
    <global-exception-mappings>
        ..............
</global-exception-mappings>

</package>

<include file=".....xml" />
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
.........
 <display-name>strutsTest</display-name>
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
 </filter>

  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
       ....
  </welcome-file-list>
 <servlet>
 <description></description>
 <display-name>Logger</display-name>
 <servlet-name>Logger</servlet-name>
 <servlet-class>...........</servlet-class>
 <init-param>
  <param-name>log4j-properties-location</param-name>
  <param-value>WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
<servlet-name>Logger</servlet-name>
<url-pattern>/Logger</url-pattern>
  </servlet-mapping>

I m not able to find any error..

what i see on screen when i run either struts action based mapping or rest url ..is :

Struts has detected an unhandled exception:
Messages:   

com.thoughtworks.xstream.XStream
com/thoughtworks/xstream/XStream
java.lang.reflect.InvocationTargetException
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
java.lang.RuntimeException: java.lang.RuntimeException:               java.lang.reflect.InvocationTargetException
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

File:   org/apache/catalina/loader/WebappClassLoader.java
Line number:    1,516

Solution

  • ive solved the problem with customMapper..

    this is my custom mapper class....

       package org.apache.struts2.rest.example;
    
        import javax.servlet.http.HttpServletRequest;
        import org.apache.struts2.dispatcher.mapper.ActionMapper;
        import org.apache.struts2.dispatcher.mapper.ActionMapping;
        import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;
        import com.opensymphony.xwork2.config.ConfigurationManager;
    
       public class CustomActionMapper extends DefaultActionMapper {
    
    
        public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
    
            return getActionMapper(request, configManager).getMapping(request, configManager);
    
        }
    
        private ActionMapper getActionMapper(HttpServletRequest request, ConfigurationManager configManager) {
    
            ActionMapping actionMapping = new ActionMapping();  
    
            parseNameAndNamespace(request.getRequestURI(), actionMapping, configManager);
    
            if (!(actionMapping.getNamespace()).contains("/rest")) {
    
                return container.getInstance(ActionMapper.class, "struts");
    
            } 
            else 
            {
                return container.getInstance(ActionMapper.class, "rest");
    
    
    }
    }
    

    ........................................... my struts.xml is as follows......

    <?xml version="1.0" encoding="UTF-8" ?>
    
    
      <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    
    
     <struts>
    <!--  Overwrite Convention -->
    
        <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myActionMapper" class="org.apache.struts2.rest.example.CustomActionMapper" />
        <constant name="struts.mapper.class" value="myActionMapper" />
        <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="example"/> 
        <constant name="struts.action.extension" value="xhtml,,xml,json,action"/>      
    <!-- overwrite action complete -->
    
    
    <!-- normal struts actions -->
    
    <package name="userlogin" extends="struts-default" namespace="/">
    
    <action name="*">
            <result name="error">/view/error/error.jsp</result>
        </action>
        ..................
    
    </package>
    
    
    
    
    <include file="..........xml" />
    <include file=".............xml" /> 
    
    
    <!-- normal stryts actions complete -->
    
    
    </struts>
    

    these are the libraries im using ..

    - asm-3.1.jar
    - asm-commons-3.1.jar
    - commons-beanutils-1.7.0.jar
    - commons-collections-3.1.jar
    - commons-fileupload-1.2.2.jar
    - commons-io-2.0.1.jar
    - commons-lang-2.5.jar
    - commons-logging-1.1.1.jar
    - commons-logging-api-1.1.jar
    - ezmorph-1.0.3.jar
    - freemarker-2.3.16.jar
    - javassist-3.11.0.GA.jar
    - json-lib-2.1-jdk15.jar
    - log4j-1.2.8.jar
    - ognl-3.0.1.jar
    - struts2-convention-plugin-2.2.3.jar
    - struts2-core-2.2.3.jar
    - struts2-json-plugin-2.2.3.jar
    - struts2-rest-plugin-2.2.3.jar
    - xpp3_min-1.1.3.4.O.jar
    - xstream-1.2.2.jar
    - xwork-core-2.2.3.jar
    

    make sure you use compatible library versions....