1

I have a simple Controller with a function.

@RestController
@CrossOrigin(origins = "http://localhost:4200", allowedHeaders = "*")
@RequestMapping("/api")
public class DockerController {

  @Autowired
private DockerService dockerService;

  @PostMapping("/docker/container/{containerName}/pause")
public boolean pauseContainer(@PathVariable String containerName) {
    dockerService.pauseContainer(containerName);
    return true;
}

}

But when i try to run it from the angular app i get

zone.js:2969 POST http://localhost:8080/api/docker/container/Env666/pause 403 () http://localhost:8080/api/docker/container/Env666/pause: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 403.

This started happening after I used the service functionallity.

  • Possible solution: https://stackoverflow.com/questions/43166984/delete-method-cors-issue-in-rest-controller/43167508#43167508 https://spring.io/guides/gs/rest-service-cors/ – zakaria amine May 18 '18 at 13:25

1 Answers1

0

I successfully reproduce the CORS error, contrary to Spring CrossOrigin Javadoc telling that :

By default the supported methods are the same as the ones to which a controller method is mapped.

I had to specify the method manually in the CrossOrigin annotation :

@CrossOrigin(origins="http://localhost:4200", allowedHeaders="*", methods={RequestMethod.POST})
yunandtidus
  • 3,847
  • 3
  • 29
  • 42