Assume that Project
is our POJO class. Following function provides to delete a row from database. It is successfully working with POSTMAN requests.
@RestController
@RequestMapping(value = "/project")
@CrossOrigin
public class ProjectController {
private final ProjectServiceImpl projectServiceImpl;
------------
@DeleteMapping
@RequestMapping("/delete/{id}")
public ResponseEntity<Boolean> deleteProject(@PathVariable Long id) {
boolean result = projectServiceImpl.delete(id);
return ResponseEntity.ok(result);
}
------------
}
But requests from Angular project are rejecting with 403 message. And following message is appearing in console screen.
After some searches. I learned, the application have to answer pre-flight
requests with 200. To provide this, following function was added to controller.
@GetMapping
@RequestMapping("/delete/{id:[0-9]+}")
public ResponseEntity.BodyBuilder retreive(@PathVariable Long id) {
return ResponseEntity.ok();
}
I used regex for request mapping because without it Spring Framework throws /project/delete/{id}
already mapped with another function. Angular get its 200OK for pre-flight
request with this way. But the application response is 406 for delete operation. Angular is sending http://localhost:8080/project/delete/2
url to the application. If I send same link without have a function for CORS. Row has id with 2 will delete successfully. Am I missing something?
Sources:
Why Angular sending OPTIONS message before DELETE
How to add CORS support to Spring Boot application
To implement server side proxy: proxy.conf.json
{
"/project/**": {
"target": "http://localhost:8080",
"secure": false
}
}
modified section in angular.json
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "issue-management:build",
"proxyConfig": "proxy.conf.json"
},
and Angular project started with ng serve --proxy-config proxy.conf.json
but result didn't change. Plus, suggestions in this article applied, again result didn't change.