Member-only story

How To Resolve CORS issue in Spring Boot application?

Gain Java Knowledge
3 min readJul 9, 2023

--

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.

app.component.ts(UI Code)
Browser — We are getting CORS policy issue while calling backend API.

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…

--

--

Gain Java Knowledge
Gain Java Knowledge

Written by Gain Java Knowledge

The Java programming language is one of the most popular languages today. Stay up to date with news, certifications, free learning resources and much more.

No responses yet