I have a web application that I am developing using Angular 2 and Spring Boot. I use the spring-boot-data-rest
dependency to expose my repositories as HTTP endpoints.
During development, I run my backend spring boot project on a local tomcat that runs on port 8080. To develop the frontend, I use the angular-cli to serve my Angular 2 application on port 4200. My frontend running on 4200 needs to be able to hit the endpoints exposed on 8080, but that doesn't work because:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
If these were custom endpoints that I manually typed in a @RestController
, I could simply add the @CrossOrigin
annotation as such:
@RestController
public class MyController {
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(value = "/whatever")
public void whatever() {
//whatever
}
}
But I obviously cannot do this for my endpoints exposed by spring-boot-data-rest
. So, how can I make those endpoints accessible from the http://localhost:4200
origin?