5

I have written a simple code to integrate rest with Struts 2.3.24

I have my Struts XML:

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
                        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.mapper.class"                      value="rest" />
    <!--  Overwrite Convention -->
    <constant name="struts.convention.action.suffix"          value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches"   value="true"/>
    <constant name="struts.convention.default.parent.package" value="com.pag.rest.service"/>
    <constant name="struts.convention.package.locators"       value="service"/>
</struts> 

And my controller class is:

package com.pag.rest.service;
public class RequestController {

    // GET 
    public String index() {
        return "SUCCESS";
    }

    // GET 
    public String show() {
        return "SUCCESS";
    }

    // POST
    public String create() {
        return "Create - SUCCESS";
    }

    // PUT 
    public String update() {
        return "SUCCESS";
    }

    // DELETE 
    public String destroy() {
        return "SUCCESS";
    }
}

Whenever I try to access the service, it says not found with action not mapped exception.

Please let me know what else I need to do in order to get the code working.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Harshal
  • 57
  • 2
  • Don't return `"SUCCESS"`: return `Action.SUCCESS` (the constant), or its value, `"success"`. Also don't return `"Create - SUCCESS".`. You probably need to read something about Struts2 result mapping. – Andrea Ligios Mar 15 '16 at 14:48
  • Can you try changing the name of your Controller ? Because Request could somehow be a reserved word, so please try with FoobarController, just to exclude it – Andrea Ligios Mar 15 '16 at 16:53
  • Did try after changing the class name , still it gives HTTP 404 .. not found error – Harshal Mar 16 '16 at 11:40

1 Answers1

0

The parent package should be rest-default. Add the following constant to the configuration file struts.xml:

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

Remove

<constant name="struts.convention.package.locators" value="service"/>

And rename your package name to com.pag.rest.actions. It will search your controllers under the actions folder.

Roman C
  • 49,761
  • 33
  • 66
  • 176