0

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.

anoop
  • 1,604
  • 6
  • 24
  • 50

1 Answers1

2

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"/>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Andrea thanks for your suggestion. But i feared like my webapplication is already built and it have 100 of actions so far in the project we haven't used struts.action.extension. So i am feared like if i add the whether it will affect my current actions. Also i haven't used any action suffixes so far for my actions. – anoop Sep 08 '15 at 08:48
  • Read **carefully** the sentence again. Since the REST plugin is overriding the default action extensions, you need to re-override it to bring it back to the one you desire (the default one, or a custom one of your... BTW it is the one you are using now). – Andrea Ligios Sep 08 '15 at 08:54
  • I have written a custom actionmapper to load the rest services in different namespace like http://stackoverflow.com/questions/17697432/struts2-rest-plugin-making-both-struts-actions-rest-actions-work-together-but. But i got a white page when i call the rest service url like http://localhost:8095/web-services/rest/employee.json – anoop Sep 08 '15 at 17:33
  • @anoop, no need to write custom mapper to map the URL, check this repo https://github.com/sivailango/struts2-rest-nonrest – Sivailango Nov 23 '15 at 08:02