Home

Enable CORS in a Spring MVC REST API for ExtJS

Published on 2/21/2013


Let’s say you want to run your ExtJS application on example.com, but run your REST API at example-api.com . 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:

       
            com.thetransactioncompany
            cors-filter
            1.3.2
       

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

   
CORS
com.thetransactioncompany.cors.CORSFilter

   cors.supportedMethods    GET, HEAD, POST, PUT, DELETE, OPTIONS    cors.supportedHeaders    Content-Type, X-Requested-With, Origin, Accept

   

   
CORS
/*
   

Your ExtJS REST proxy should now work!

... views