Friday, June 1, 2012

Enable Cross Origin Resource Sharing for Jersey


In this blog, we will talk about how to enable and configure CORS support for Jersey, and more importantly, how to trouble shoot if CORS is not working properly.

As mentioned in the previous blog, we were disappointed to find out Apache CXF CORS support did not work and were pleasantly surprised on how easy CORS filter has been to setup and configure. We have tested CORS filter against Jersey, RESTeasy, and Apache CXF and it worked for every single one of them.

Enable CORS Support

CORS filter is implemented as a Servlet that must be enabled and configured at the web app's level, inside web.xml file.
Here is a sample web.xml file,
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
    <servlet-name>Jersey Root REST Service</servlet-name>
    <servlet-name>Jersey Root REST Service</servlet-name>
  <!-- Note: All parameters are options, if ommitted CORS Filter
       will fall back to the respective default values.
   <param-value>GET, HEAD, POST, OPTIONS, PUT, DELETE</param-value>
   <param-value>Content-Type, X-Requested-With, Accept, Authentication</param-value>
   <param-value>X-Test-1, X-Test-2</param-value>


  <!-- CORS Filter mapping -->

Configuring CORS Filter

The default configuration values are good for everything except for the following two fields, 
  • cors.supportedMethods
  • cors.supportedHeaders
These two fields must be checked if CORS filter is not working as one expected.

cors.supportedMethods specifies a list of supported methods and the default value is GET, HEAD, and POST only. We recommend listing all HTTP methods as supported methods.

cors.supportedHeaders lists the set of supported header fields. This set must be expanded if more headers are passed in unexpected.  We recommend listing as many headers as possible.

Testing and Troubleshooting

CORS support can be tested through javascript and here is an example,
<title>Cors Example</title> 
<script type="text/javascript" src=""></script>
<script type="text/javascript" src="log4javascript.js"></script>
var hello = JSON.stringify({"greeting":"Hello","name":"jersey"});
$(document).ready(function() {

 //alert('before ajax call');
  headers: {
   Authentication : 'Bearer access_token'
  //this is the php file that processes the data and send mail
  //url: "http://localhost:8080/cxf-hello-cors/rest/annotatedGet/hello", 
  //url: "http://localhost:8080/cxf-hello-cors/service1/time", 
  // url: "http://localhost:8080/resteasy/tutorial/helloworld",
  url: "http://localhost:8080/jersey/hello",
  contentType: "application/json",

  //GET method is used
  type: 'DELETE',
  //pass the data         
  dataType: 'json',   
  //data: JSON.stringify(hello), 
  data: hello,
  //Do not cache the page
  cache: false,
  success: function (html) {  
   document.getElementById("cors").innerHTML = "Echo: " + html.greeting + "," +; 
  } ,
  error:function (data, status) {

<h1>This is the CORS test page</h1>

<p>Hello, <div id="cors"/>


Troubleshooting CORS

We use a combination of Tomcat access log, Firefox Firebug, and Jersey client to troubleshoot CORS support.
CORS relies on header to relay cross origin  resource sharing information back to the browser and CORS-supported browser will enforce CORS based on these header fields. When CORS is not working as expected, the majority of the errors happen when Web Services do not pass back the appropriate headers due to permission related issues, like supported headers or supported methods. The best place to look for this type of information is in Tomcat's access log.
Here are some sample entries from the access log, - - [31/May/2012:15:40:42 -0400] "GET /jersey/hello HTTP/1.1" 401 - - name [31/May/2012:15:42:27 -0400] "GET /jersey/hello HTTP/1.1" 200 36 - - [31/May/2012:15:42:39 -0400] "GET /jersey/hello HTTP/1.1" 401 - - - [31/May/2012:15:44:18 -0400] "GET /jersey/hello HTTP/1.1" 401 - - - [31/May/2012:15:45:45 -0400] "GET /jersey/hello HTTP/1.1" 401 - - - [31/May/2012:15:46:38 -0400] "GET / HTTP/1.1" 401 - - - [31/May/2012:15:46:52 -0400] "GET /jersey/hello HTTP/1.1" 401 -
0:0:0:0:0:0:0:1%0 - - [31/May/2012:15:47:02 -0400] "OPTIONS /jersey/hello HTTP/1.1" 403 94 - - [31/May/2012:15:48:06 -0400] "GET / HTTP/1.1" 401 -
0:0:0:0:0:0:0:1%0 - - [31/May/2012:15:51:23 -0400] "OPTIONS /jersey/hello HTTP/1.1" 200 -
0:0:0:0:0:0:0:1%0 - name [31/May/2012:15:51:23 -0400] "DELETE /jersey/hello HTTP/1.1" 200 36 - name [31/May/2012:16:01:12 -0400] "GET /jersey/hello HTTP/1.1" 200 36 
Each entry represents an access from the client. The last three entries represent the following, request URI, HTTP status code, return content length. If CORS is not working as expected, check the following,
  • Make sure there is an entry in the access log that corresponds to the request
  • Make sure HTTP status code is correct. If HTTP status code is 403, check CORS filter's
    supported methods and supported headers to make sure that both settings are configured
If HTTP code is 200 but CORS is still not working, turn Firebug on and examine the request
pay special attention to response headers,
Debugging CORS response with Firebug

Make sure the set of Access-Control-* headers present in response.


  1. Are you supporting internet explorer 8 or 9? If so, I'm curious how you got around its cross origin limitations.

    1. Hi Brandon,
      Since I a Mac user, I tested CORS filter against Firefox and Safari. It seems like CORS is well supported for IE 10, .
      My colleague mentioned that you need to install another plugin along with JQuery in order to the CORS support to work. There is no change on the server side, as the returned HTTP headers are pretty much standard. Please share your experience.

      Here is another thread on IE9 CORS support,

    2. Try this, .
      Let us know how it works.

  2. I'm on an android app with phonegap and RestEasy. I can't access the web service from the android client application. My xmlhttp.status is 0 and the xmlhttp.responseText is undefined.

    Can you help please

    1. There are a couple of possibilities,
      1. Your android client application's request never makes it to the Tomcat server. Please check your Tomcat access log to verify that it did receive the message
      2. If Tomcat receives the message and your RestEasy class responds correctly, you need to check and verify that the CORS filter returns the correct Headers
      3. Check your CORS filter configuration to make sure that the accepted headers include things like "Content-Type, X-Requested-With, Accept, Authentication, Origin"

      Let me know what you find out.

  3. very informative post sharing with us thanks for the sharing
    see more

  4. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java EE Online Training Students.