<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hemant Rai</title>
    <description>The latest articles on DEV Community by Hemant Rai (@hemant_rai_1985).</description>
    <link>https://dev.to/hemant_rai_1985</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3816268%2Fbbbaf293-b4aa-43d3-8b73-ff396a66dad4.jpg</url>
      <title>DEV Community: Hemant Rai</title>
      <link>https://dev.to/hemant_rai_1985</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hemant_rai_1985"/>
    <language>en</language>
    <item>
      <title>Spring Boot BeanCreationException – Common Causes and Fixes</title>
      <dc:creator>Hemant Rai</dc:creator>
      <pubDate>Tue, 10 Mar 2026 14:25:02 +0000</pubDate>
      <link>https://dev.to/hemant_rai_1985/spring-boot-beancreationexception-common-causes-and-fixes-45m0</link>
      <guid>https://dev.to/hemant_rai_1985/spring-boot-beancreationexception-common-causes-and-fixes-45m0</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When developing applications using Spring Boot, you may encounter an error like this during application startup:&lt;/p&gt;

&lt;p&gt;org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService'&lt;/p&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;UnsatisfiedDependencyException: Error creating bean with name&lt;/p&gt;

&lt;p&gt;These errors occur when the Spring container fails to create or inject a bean into the application context.&lt;/p&gt;

&lt;p&gt;In this article, we will understand:&lt;/p&gt;

&lt;p&gt;• What BeanCreationException is&lt;br&gt;&lt;br&gt;
• Why Spring fails to create beans&lt;br&gt;&lt;br&gt;
• Common solutions to fix the problem&lt;/p&gt;
&lt;h2&gt;
  
  
  What is a Spring Bean?
&lt;/h2&gt;

&lt;p&gt;In Spring, a &lt;strong&gt;bean&lt;/strong&gt; is an object that is managed by the Spring IoC (Inversion of Control) container.&lt;/p&gt;

&lt;p&gt;Spring automatically creates and manages beans when classes are annotated with annotations such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;@Component&lt;/li&gt;
&lt;li&gt;@Service&lt;/li&gt;
&lt;li&gt;@Repository&lt;/li&gt;
&lt;li&gt;@Controller&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These beans are then injected into other classes using dependency injection.&lt;/p&gt;
&lt;h2&gt;
  
  
  Example Error
&lt;/h2&gt;

&lt;p&gt;During application startup, you might see an error like this:&lt;/p&gt;

&lt;p&gt;org.springframework.beans.factory.BeanCreationException:&lt;br&gt;
Error creating bean with name 'userService':&lt;br&gt;
Unsatisfied dependency expressed through field&lt;br&gt;
This means Spring could not create or inject the required bean.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cause 1: Missing @Component or @Service Annotation
&lt;/h2&gt;

&lt;p&gt;Spring only creates beans for classes that are registered in the application context.&lt;/p&gt;

&lt;p&gt;If a class is missing annotations like &lt;code&gt;@Component&lt;/code&gt;, &lt;code&gt;@Service&lt;/code&gt;, or &lt;code&gt;@Repository&lt;/code&gt;, Spring will not create the bean.&lt;/p&gt;

&lt;p&gt;Example problem:&lt;br&gt;
public class UserService {&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public String getUser() {
    return "John";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;In this case, Spring does not recognize the class as a bean.&lt;/p&gt;
&lt;h3&gt;
  
  
  Fix
&lt;/h3&gt;

&lt;p&gt;Add the &lt;code&gt;@Service&lt;/code&gt; annotation:&lt;/p&gt;

&lt;p&gt;@Service&lt;br&gt;
public class UserService {&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public String getUser() {
    return "John";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;h2&gt;
  
  
  Cause 2: Package Scanning Problem
&lt;/h2&gt;

&lt;p&gt;Spring Boot automatically scans components starting from the package where the main application class is located.&lt;/p&gt;

&lt;p&gt;If your beans are located outside that package structure, Spring may fail to detect them.&lt;/p&gt;

&lt;p&gt;@SpringBootApplication&lt;br&gt;
public class Application {&lt;br&gt;
    public static void main(String[] args) {&lt;br&gt;
        SpringApplication.run(Application.class, args);&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;If your service classes are placed in a different package not scanned by Spring, the beans will not be created.&lt;/p&gt;
&lt;h3&gt;
  
  
  Fix
&lt;/h3&gt;

&lt;p&gt;Ensure that your components are placed under the same base package as your main Spring Boot application class.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cause 3: Multiple Beans of Same Type
&lt;/h2&gt;

&lt;p&gt;If multiple beans of the same type exist, Spring may not know which one to inject.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailService&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;

&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SmsService&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When injecting &lt;code&gt;NotificationService&lt;/code&gt;, Spring finds multiple candidates and throws an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix
&lt;/h3&gt;

&lt;p&gt;Use the &lt;code&gt;@Qualifier&lt;/code&gt; annotation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Autowired&lt;/span&gt;
&lt;span class="nd"&gt;@Qualifier&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"emailService"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt; &lt;span class="n"&gt;notificationService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cause 4: Circular Dependency
&lt;/h2&gt;

&lt;p&gt;A circular dependency occurs when two beans depend on each other.&lt;/p&gt;

&lt;p&gt;For example, Service A depends on Service B, and Service B depends on Service A.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ServiceA&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ServiceB&lt;/span&gt; &lt;span class="n"&gt;serviceB&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ServiceB&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ServiceA&lt;/span&gt; &lt;span class="n"&gt;serviceA&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a circular dependency that prevents Spring from properly initializing the beans.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix
&lt;/h3&gt;

&lt;p&gt;Use constructor injection or redesign the dependency structure to remove the circular reference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices to Avoid Bean Creation Errors
&lt;/h2&gt;

&lt;p&gt;To avoid BeanCreationException in Spring Boot applications:&lt;/p&gt;

&lt;p&gt;• Always annotate service classes with &lt;code&gt;@Service&lt;/code&gt;, &lt;code&gt;@Component&lt;/code&gt;, or &lt;code&gt;@Repository&lt;/code&gt;&lt;br&gt;&lt;br&gt;
• Keep your classes inside the base package scanned by Spring Boot&lt;br&gt;&lt;br&gt;
• Avoid circular dependencies between services&lt;br&gt;&lt;br&gt;
• Use &lt;code&gt;@Qualifier&lt;/code&gt; when multiple beans of the same type exist&lt;br&gt;&lt;br&gt;
• Prefer constructor injection instead of field injection****&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Bean creation errors are common when working with Spring Boot applications.&lt;/p&gt;

&lt;p&gt;Most issues occur due to missing annotations, incorrect package scanning, multiple beans of the same type, or circular dependencies.&lt;/p&gt;

&lt;p&gt;By understanding these common causes, developers can quickly diagnose and fix BeanCreationException errors during application startup.&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>java</category>
      <category>programming</category>
      <category>backend</category>
    </item>
    <item>
      <title>How to Fix CORS Issue in Spring Boot (3 Easy Solutions)</title>
      <dc:creator>Hemant Rai</dc:creator>
      <pubDate>Tue, 10 Mar 2026 07:53:23 +0000</pubDate>
      <link>https://dev.to/hemant_rai_1985/how-to-fix-cors-issue-in-spring-boot-3-easy-solutions-2o3p</link>
      <guid>https://dev.to/hemant_rai_1985/how-to-fix-cors-issue-in-spring-boot-3-easy-solutions-2o3p</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When building modern web applications, the frontend and backend often run on different ports or domains.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Frontend: &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Backend: &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;When the frontend tries to call the backend API, you may see the following error in the browser console:&lt;/p&gt;

&lt;p&gt;Access to fetch at '&lt;a href="http://localhost:8080/api/users" rel="noopener noreferrer"&gt;http://localhost:8080/api/users&lt;/a&gt;'&lt;br&gt;
from origin '&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;'&lt;br&gt;
has been blocked by CORS policy&lt;/p&gt;

&lt;p&gt;This error occurs because of &lt;strong&gt;CORS (Cross-Origin Resource Sharing)&lt;/strong&gt; restrictions enforced by browsers.&lt;/p&gt;

&lt;p&gt;In this article, we will understand:&lt;/p&gt;

&lt;p&gt;• What CORS is&lt;br&gt;&lt;br&gt;
• Why this error occurs&lt;br&gt;&lt;br&gt;
• How to fix CORS issues in Spring Boot&lt;/p&gt;
&lt;h2&gt;
  
  
  What is CORS?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fak0i0f8yot8wnoafhkra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fak0i0f8yot8wnoafhkra.png" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CORS (Cross-Origin Resource Sharing) is a security feature implemented by web browsers.&lt;/p&gt;

&lt;p&gt;It prevents a web application running on one origin from making requests to a resource on another origin unless the server explicitly allows it.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;origin&lt;/strong&gt; is defined by three parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Protocol (http / https)&lt;/li&gt;
&lt;li&gt;Domain&lt;/li&gt;
&lt;li&gt;Port&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Frontend → &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Backend → &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;Even though both run on localhost, the &lt;strong&gt;ports are different&lt;/strong&gt;, so the browser treats them as &lt;strong&gt;different origins&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Because of this, the browser blocks the request unless the backend enables CORS.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Does the CORS Error Occur?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftia1y7iqro07hnmcetq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftia1y7iqro07hnmcetq2.png" alt=" " width="789" height="697"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a frontend application tries to call an API hosted on a different origin, the browser first sends a &lt;strong&gt;preflight request&lt;/strong&gt; to check if the server allows the request.&lt;/p&gt;

&lt;p&gt;If the backend does not include the correct CORS headers in the response, the browser blocks the request.&lt;/p&gt;

&lt;p&gt;This results in an error like:&lt;/p&gt;

&lt;p&gt;"Blocked by CORS policy"&lt;/p&gt;
&lt;h2&gt;
  
  
  What You Should Do Now
&lt;/h2&gt;
&lt;h2&gt;
  
  
  Solution 1: Using @CrossOrigin Annotation
&lt;/h2&gt;

&lt;p&gt;The easiest way to fix CORS issues in Spring Boot is by using the &lt;code&gt;@CrossOrigin&lt;/code&gt; annotation.&lt;/p&gt;

&lt;p&gt;This annotation allows cross-origin requests for a specific controller or endpoint.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/api"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@CrossOrigin&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;origins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"http://localhost:3000"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUsers&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;@CrossOrigin&lt;/code&gt; annotation allows requests from &lt;code&gt;http://localhost:3000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This means a frontend application running on port &lt;strong&gt;3000&lt;/strong&gt; can access the Spring Boot API.&lt;/p&gt;

&lt;p&gt;You can also allow multiple origins if needed.&lt;/p&gt;

&lt;p&gt;⚠️ Note: Avoid using &lt;code&gt;@CrossOrigin(origins = "*")&lt;/code&gt; in production because it allows requests from any domain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 2: Global CORS Configuration
&lt;/h2&gt;

&lt;p&gt;If your application has many controllers, adding &lt;code&gt;@CrossOrigin&lt;/code&gt; to each controller can become repetitive.&lt;/p&gt;

&lt;p&gt;A better approach is to configure CORS globally using &lt;code&gt;WebMvcConfigurer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.annotation.Configuration&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.servlet.config.annotation.CorsRegistry&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.servlet.config.annotation.WebMvcConfigurer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WebConfig&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;WebMvcConfigurer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;addCorsMappings&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CorsRegistry&lt;/span&gt; &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/**"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;allowedOrigins&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://localhost:3000"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;allowedMethods&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"PUT"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"DELETE"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;allowedHeaders&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"*"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration enables CORS for all endpoints in the application.&lt;/p&gt;

&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;addMapping("/**")&lt;/code&gt; → applies CORS to all API endpoints
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;allowedOrigins&lt;/code&gt; → specifies which frontend domains can access the API
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;allowedMethods&lt;/code&gt; → defines the allowed HTTP methods
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach is recommended for larger applications where many controllers exist.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 3: Fix CORS When Using Spring Security
&lt;/h2&gt;

&lt;p&gt;If your Spring Boot application uses Spring Security, CORS must also be enabled in the security configuration.&lt;/p&gt;

&lt;p&gt;Otherwise, Spring Security may block cross-origin requests even if CORS is configured elsewhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Bean&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;SecurityFilterChain&lt;/span&gt; &lt;span class="nf"&gt;filterChain&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpSecurity&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;http&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;cors&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;and&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;csrf&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;disable&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;cors()&lt;/code&gt; configuration enables CORS support inside Spring Security.&lt;/p&gt;

&lt;p&gt;Without this configuration, Spring Security may override your CORS settings and block requests.&lt;/p&gt;

&lt;p&gt;Disabling CSRF is common for REST APIs, especially when authentication is handled using tokens.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Handling CORS
&lt;/h2&gt;

&lt;p&gt;When configuring CORS in production applications, consider the following best practices:&lt;/p&gt;

&lt;p&gt;• Avoid allowing all origins (&lt;code&gt;*&lt;/code&gt;) in production&lt;br&gt;&lt;br&gt;
• Only allow trusted frontend domains&lt;br&gt;&lt;br&gt;
• Use global configuration for larger applications&lt;br&gt;&lt;br&gt;
• Test CORS configuration carefully when using Spring Security&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;CORS errors are common when frontend and backend applications run on different origins.&lt;/p&gt;

&lt;p&gt;In Spring Boot, you can resolve CORS issues using:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;@CrossOrigin&lt;/code&gt; annotation for specific controllers
&lt;/li&gt;
&lt;li&gt;Global CORS configuration using &lt;code&gt;WebMvcConfigurer&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Enabling CORS support in Spring Security
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Choosing the right approach depends on your application architecture.&lt;/p&gt;

&lt;p&gt;By configuring CORS properly, you can allow secure communication between your frontend and backend applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is CORS?&lt;/li&gt;
&lt;li&gt;Why Does the CORS Error Occur?&lt;/li&gt;
&lt;li&gt;Solution 1: Using @CrossOrigin&lt;/li&gt;
&lt;li&gt;Solution 2: Global CORS Configuration&lt;/li&gt;
&lt;li&gt;Solution 3: Fix CORS When Using Spring Security&lt;/li&gt;
&lt;li&gt;Best Practices&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 Developer Tip:&lt;/p&gt;

&lt;p&gt;If you are developing locally with React or Angular, you can also configure a proxy to avoid CORS issues during development.&lt;/p&gt;

&lt;p&gt;A step-by-step guide to fixing "Blocked by CORS policy" errors in Spring Boot applications.&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>java</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
