当我在struts2 + spring3项目中添加struts2-rest-plugin时,struts action没有采用默认方法(执行)

时间:2013-09-24 05:31:50

标签: java spring rest struts2

我正在尝试使用struts2创建一个restful服务。当我在依赖struts动作中添加struts2-rest-plugin时没有采用默认方法(执行)。它正在寻找create()或index()方法。

struts2-convention-plugin和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>

我在 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"/>

我在执行网络项目时收到错误

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)

我的项目有依赖管理的春天,使用我们计划使用的弹簧MCV使用struts2.I试图使用spring restful服务但我得到了一些调度错误所以我转移到struts2-rest-plugin。这里作为服务实现的控制器类具有类似于HTTP GET和PUT的index()和create()的方法。其余插件的工作原理。

2 个答案:

答案 0 :(得分:2)

请阅读文档[1] [2],如果您想将普通网络应用与其他服务混合,请考虑将所有其余端点移动到struts.rest.namespace的专用命名空间中[3]

[1] http://struts.apache.org/development/2.x/docs/rest-plugin.html

[2] http://struts.apache.org/development/2.x/docs/convention-plugin.html#ConventionPlugin-ConvertingaCodebehindbasedapplicationtoConvention

[3] http://struts.apache.org/development/2.x/docs/rest-plugin.html#RESTPlugin-Settings

答案 1 :(得分:1)

使用struts2-rest插件以及convention-plugin意味着您的操作类不再具有执行方法作为默认值。实际上,您可能没有执行方法,但仍然拥有有效的REST-ful控制器。

如何将URL映射到类的方式由convention-plugin定义。这在这里有清楚的解释:http://struts.apache.org/docs/convention-plugin.html

此外,struts.rest.namespace - 方法对我没有用。不知何故,Struts无法以这种方式工作。相反,我做了以下几点: -

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

这样我欺骗了Struts,感觉我的REST Web服务调用的名称空间是/rest