Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Eliminar problema de Method Cors en Rest Controller

Tengo algunos puntos finales Rest en mi proyecto a los que llamo desde una aplicación cliente en otro servidor. He deshabilitado con éxito Cors usando la anotación @CrossOrigin , y todos los métodos funcionan bien, excepto el método Eliminar, que arroja el siguiente error en Chrome:

XMLHttpRequest cannot load http://localhost:8856/robotpart/1291542214/compatibilities. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8888' is therefore not allowed access. The response had HTTP status code 403.

Aquí está mi controlador:

 @CrossOrigin(origins = "*") @ExposesResourceFor(RobotPart.class) public class RobotPartController { //All endpoints are working except the Delete Mapping @GetMapping("/robotpart") public ResponseEntity<List<RobotPartResource>> listAllParts() { //.. } @GetMapping("/robotpart/{id}") public ResponseEntity<RobotPartResource> getById(@PathVariable Integer id) { //.. } @GetMapping("/robotpart/{id}/compatibilities") public ResponseEntity<Collection<RobotPartResource>> getRobotCompatibilities(@PathVariable Integer id, //.. } @PostMapping("/robotpart") public ResponseEntity<RobotPartResource> getById(@RequestBody @Valid RobotPart newRobot) { //.. @PutMapping("/robotpart/{id}") public ResponseEntity<RobotPartResource> modify(@PathVariable Integer id, @Valid @RequestBody RobotPart newRobot) { //... } @DeleteMapping("/robotpart/{id}") public ResponseEntity<RobotPart> deleteById(@PathVariable Integer id) { //... } }

¿Alguna forma de evitarlo?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Encontré una solución, después de analizar las solicitudes http, noté que al encabezado Access-Control-Allow-Methods le faltaba el método DELETE, así que lo agregué eliminando la anotación @CrossOrigin y agregando este bean a la configuración:

 @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/robotpart/**").allowedOrigins("*").allowedMethods("GET", "POST","PUT", "DELETE"); } }; }
about 4 years ago · Santiago Trujillo Report

0

Agregando a las respuestas anteriores, la razón por la cual deshabilitar CORS no funcionará para DELETE (pero funciona para GET y POST) es que este es el comportamiento predeterminado para WebMvcConfigurer como se indica aquí (resaltado en amarillo):

ingrese la descripción de la imagen aquí

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!