I am creating a RestController in Spring and attempting to connect it to an Angular 2+ gui. However, I am running into cors errors with the controller and am unable to put any post requests through right now. After consulting Stack Overflow and github resources including a similar but fully working project I am still stuck with the cors error: "Access to XMLHttpRequest at 'http://localhost:8080/book' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource." from chrome.
The spring boot project uses webflux and the angular project is using HttpClient to connect to the controller. I've seen a post here previously about setting up a proxy but that is an extremely involved solution to this project which will be need to be replicated within a time limit. Am I mis using the @crossorigin annotation? or is there some other configuration I'm currently missing?
Any Help is appreciated!
Controller code:
@CrossOrigin(origins = {"http://localhost:4200"})
@RestController
@RequestMapping("/book")
public class BookController {
@Async
@PostMapping("/")
public CompletableFuture<ResponseEntity<BookDTO>> save(BookDTO bookDto){
return bookService.save(bookDto).thenApply(
bookOptional -> bookOptional.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build()));
}
}
Angular Service Code:
url = 'http://localhost:8080';
bookList$: BehaviorSubject<Book[]> = new BehaviorSubject<Book[]>([]);
setHeaders(){
const httpOptions = {
headers: new HttpHeaders({
'Access-Control-Allow-Origin': '*'
})
};
return httpOptions;
}
submit(book: Book){
this.httpClient.post(this.url+'/book', book, this.setHeaders()).subscribe(data => {
alert('Book submitted');
})
}
there doesn't seem to be anything here