I have a Spring Boot app and I wanted to allow other origins to make requests because I got the ‘cors’ error. So I searched and I found this answer: Annotation CrossOrigin not working in Spring boot witch helped for the endpoints that have no body. On the other hand, those who have a body I get Internal Server Error
.
Below is the configuration:
@Configuration @EnableWebMvc public class CorsConfiguration implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://localhost:63343", "http://localhost:3000", "https://smart-booking-ba548.web.app") .allowedMethods("GET", "POST", "DELETE", "PUT"); } }
The controllers have the @RestController
annotation and the methods @Get/Post|Mapping
. They return a ResponseEntity<Object>
.
Advertisement
Answer
I solved it by putting the following method in main class.
@Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedOrigins("*"); } }; }