Member-only story
How To Resolve CORS issue in Spring Boot application?
In this article we will learn How to fix CORS issue while we are calling Spring Boot Rest API from Angular UI.
I have already create one Spring boot application and UI service at my local. In my Spring boot application I have just created One rest endpoint(GET : http://localhost:8080/message)
that I am calling from our UI.
To Resolve or Fix CORS issue there are multiple ways. But today we will learn how to define global configuration to Fix CORS issue. We just need to go to our Spring Boot Application and Here we need to create one new config class at class level we will use @Configuration annotation.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SecurityConfiguration {
@Value("${cors.allowedMethods}")
private String allowedMethods;
@Value("${cors.allowedHeaders}")
private String allowedHeaders…