Thursday, February 21, 2013

Enable CORS in a Spring MVC REST API for ExtJS

Let's say you want to run your ExtJS application on, but run your REST API at . Normally, you cannot do this without resorting to JSONP (GETs only), or a server proxy, due to security restrictions. However, it can be done safely using CORS, which modern browsers support.

I've been using Java Spring Roo for creating my REST API. Unfortunately, it does not support CORS out of the box. Here's how to set it up.

In your maven pom.xml file, inside dependencies, add:


Open src/main/webapp/WEB-INF/web.xml and add:


   <param-value>GET, HEAD, POST, PUT, DELETE, OPTIONS</param-value>

   <param-value>Content-Type, X-Requested-With, Origin, Accept</param-value>



Your ExtJS REST proxy should now work!

1 comment: