title: Using Spring controllers inside Struts 2 to create a REST API
body: <p>I have an application that uses Struts (version 2.3.16.1 GA) and Spring (version 3.2.4 GA) to inject objects into Actions, etc.</p>
  <p>Now, I need to create a REST API for my app, and I can't use Struts' REST plugin for that, so I thought that since Spring was around already, I could just create Spring Controllers and then serve the API with Spring.</p>
  <p>Say my current app runs in the <code>app</code> context on the server, so I will access it like this:</p>
  <pre><code>http://www.myserver.com:8080/app/some/path/anAction.action
  </code></pre>
  <p>And then that's how the whole flow of my current app works. Now what I'm intending to do, is to create a specific path which will be used exclusively by my REST API, so I tried making it work like this for example:</p>
  <pre><code>http://www.myserver.com:8080/app/api/users
  </code></pre>
  <p>I want to map <code>/api/*</code> to Spring, so all my controllers will live there.</p>
  <p>Here are the relevant bits of my config that I did in order to accomplish this.</p>
  <p><strong><code>web.xml</code>:</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;filter&gt;
      &lt;filter-name&gt;struts-prepare&lt;/filter-name&gt;
      &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;struts-execute&lt;/filter-name&gt;
      &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;struts-prepare&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
      &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
  &lt;/filter-mapping&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;struts-execute&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
      &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
  &lt;/filter-mapping&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;rest-api&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextClass&lt;/param-name&gt;
          &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;com.myapp.spring.SpringApplicationConfiguration&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
       &lt;servlet-name&gt;rest-api&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/api/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  <p><strong><code>struts.xml</code>:</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;constant name=&quot;struts.action.excludePattern&quot; value=&quot;/api/*&quot;/&gt;
  </code></pre>
  <p><strong><code>SpringApplicationConfiguration.java</code>:</strong></p>
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableWebMvc
  @ComponentScan( basePackages = {&quot;com.myapp.controller&quot;})
  @Import( {BaseConfig.class, DataBaseConfiguration.class})
  public class SpringApplicationConfiguration {}
  </code></pre>
  <p><strong><code>UserController.java</code>:</strong></p>
  <pre class="lang-java prettyprint-override"><code>@Controller
  public class UserController {
  
      @Autowired
      private UserService service;
  
      @RequestMapping(value=&quot;/users&quot;, method=GET)
      @ResponseBody public List&lt;User&gt; list(@RequestParam(defaultValue = &quot;25&quot;) int limit, @RequestParam(defaultValue = &quot;1&quot;) int offset,  @RequestParam String filter ) {
          return service.list(limit, offset, filter);
      }
  }
  </code></pre>
  <p>That's all the configuration I think I should have. Now, I start the server, login into the app (so check that Struts keep working fine), then try to send a GET request into my REST endpoint, and here are the logs:</p>
  <p><strong>Logs:</strong></p>
  <pre><code>INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-2) FrameworkServlet 'rest-api': initialization completed in 8939 ms
  INFO  [org.jboss.web] (MSC service thread 1-2) JBAS018210: Registering web context: /app
  INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed &quot;app.ear&quot;
  INFO  [com.app.web.filter.SignOnFilter] (http--0.0.0.0-8443-5) Redirecting to signon: /signon.action?redirectUri=index.action
  WARN  [org.springframework.web.servlet.PageNotFound] (http--0.0.0.0-8443-3) No mapping found for HTTP request with URI [/app/api/users] in DispatcherServlet with name 'rest-api'
  </code></pre>