I want to add this header "Access-Control-Allow-Origin", "*" to every response made to the client whenever a request has made for rest controllers in my application to allow cross origin resource sharing Currently I 'm manually adding this header to each and every method like this
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
Its working but its very frustrating . I found webContentInterceptor in spring docs which allow us to modify headers on each response
<mvc:interceptors>
<bean id="webContentInterceptor"
class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="Access-Control-Allow-Origin" value="*"/>
</bean>
</mvc:interceptors>
but when i use this it throws error that property not found of name Access-Control-Allow-Origin so is there any other way we can automatically add header to every response
Update ! Spring framework 4.2 greatly simplifies this by adding @CrossOrigin annotation to either a method or a controller itself https://spring.io/blog/2015/06/08/cors-support-in-spring-framework