Enable CORS Cross Origin Requests RESTful Web Service

Enable CORS Cross Origin Requests RESTful Web Service

CORS (Cross-origin resource sharing) is a technique which allows resources to access service on web page that is coming from different domain. Client side code makes an AJAX calls where they use XMLHttpRequest object to make an request to the service and this kind of Cross-domain request will be forbidden by web browsers as per their same-origin security policy.
CORS is a technique which defines way through which browser and server could interact to decide whether to allow the cross origin request or not.
While developing RESTFul web service it is required to enable Cross origin resource sharing (CORS) so that consumer which consumes the service from different domain can access it without any issue. Please follow steps below to enable CORS to RESTFul web service:

To enable CORS in Spring MVC RESTFul service you will have do below:

  • Create class: RESTFulCORSFilter.java

  • Add this class as filter in web.xml as below:

  • Sample project structure:

Enable CORS Cross Origin Requests RESTful Web Service

  • web.xml:

  • For more inforation about CORS please refer this tutorial from spring 

download Download Project: SpringMVCRESTFulService

Leave a Reply

Your email address will not be published. Required fields are marked *