Reputation: 1614
I have a web application in Struts and Hibernate that is working correctly.
We are also on the app development, and we are planning to configure both RESTful web services and web application URLs in one single struts.xml.
For web application the parent package should be
<constant name="struts.convention.default.parent.package" value="struts2"/>
But for writing the web services they are saying like the parent package should be write like this
<constant name="struts.convention.default.parent.package" value="rest-default" />
How can I accommodate both the parent package to make the web application and the web services working together ?
Also, what all other configuration I need to add to write RESTful webservices using Struts2 ?
My updated struts.xml is
<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"/>
It is also not working. The actions are working clearly in this case. but the web services are not working.
Upvotes: 0
Views: 3065
Reputation: 50203
You need the REST plugin along with the Convention plugin (even if the latter is suggested, but not mandatory).
According to the documentation:
Configuration ( struts.xml )
Just dropping the plugin's into your application may not produce exactly the desired effect. There are a couple of considerations. The first consideration is whether you want to have any non-RESTful URL's coexisting with your RESTful URL's. We'll show two configurations. The first assumes all you want to do is REST. The second assumes you want to keep other non-RESTful URL's alive in the same Struts 2 application.
and then
REST and non-RESTful URL's Together Configuration
If you want to keep using some non-RESTful URL's alongside your REST stuff, then you'll have to provide for a configuration that utilizes to mappers.
Plugins contain their own configuration. If you look in the Rest plugin jar, you'll see the struts-plugin.xml and in that you'll see some configuration settings made by the plugin. Often, the plugin just sets things the way it wants them. You may frequently need to override those settings in your own struts.xml.
First, you'll need to re-assert the extensions that struts knows about because the rest plugin will have thrown out the default action extension.
<constant name="struts.action.extension" value="xhtml,,xml,json,action"/>
Next, we will configure the PrefixBasedActionMapper, which is part of the core Struts 2 distribution, to have some URL's routed to the Rest mapper and others to the default mapper.
<constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" /> <constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts" />
And, again, we're relying on the Convention plugin to find our controllers, so we need to configure the convention plugin a bit:
<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"/>
Upvotes: 2